Reputation: 188
assume that i'm having a URL like this
testingQuiz.com/my_test_link.php?id=4&name=test
Now I need to rewrite the above URL like this,
testingQuiz.com/4/test.html
My question is, how can I achieve that by .htaccess URL rewriting?
Upvotes: 2
Views: 1168
Reputation: 33823
Something like the following perhaps should work, this is not fully tested btw but preliminary tests show it does as expected - though perhaps the second style redirect could be refined.
/* turn on url rewriting */
RewriteEngine On
/* set the level for the rewriting, in this case the document root */
RewriteBase /
/* match 2 parameters in querystring - the first is numeric and the second is alphanumeric with certain other common charachters */
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)\.html$ my_test_link.php?id=$1&name=$2 [NC,L]
That should allow you to write your urls / links in the form http://www.example.com/23/skidoo.html
and for the $_GET
variables to be interpreted as:
$_GET['id'] => 23, $_GET['name'] => skidoo
If you need to automagically redirect the user from your original style querystring to the new, nicer style url you could try adding the following after the rule above:
RewriteCond %{THE_REQUEST} /(?:my_test_link\.php)?\?id=([^&\s]+)&name=([^&\s]+) [NC]
RewriteRule ^ %1/%2\.html? [R=302,L,NE]
That way, if a user enters the url http://www.example.com?id=23&name=skidoo
it will redirect to http://www.example.com/23/skidoo.html
Upvotes: 2