Darryl Browne

Like Me? Follow Me.

Normal procedure for using mod_rewrite is to translate friendly url's into something your underlying server technology can interpret. Something like:

RewriteRule ^blog-blog-title-(14)\.html$ blog.php?id=$1 [L]

But recently I had to do the process in reverse which seemed straightforward:

RewriteCond %{QUERY_STRING} id=14
RewriteRule ^blog\.php$ blog-blog-title-14.html [L]

But when the rule was invoked, this happened:

blog-blog-title-14.html?id=14

mod_rewrite was attaching the query string automatically, assuming this was the behaviour I was looking for. After spending a long time Googling various things to try and find an answer (and finding precious little to help me, even on webmaster help forums such as ExpertsExchange) I came up with nothing. It's almost as if nobody had attempted this before.

After some intensive really obscure searches I managed to find the not so straightforward solution which I could not find anywhere in the Apache docs. The solution is to stick this string (I have no idea what it does or why it works, but it does) after your mapped url:

${pages:%1¦NULL}?

Example:

RewriteCond %{QUERY_STRING} id=14
RewriteRule ^blog\.php$ blog-blog-title-14.html${pages:%1¦NULL}? [L]

This little string is nowhere near publicised enough nor is it easily searchable on Google, so I hope that I can help to spread the word by mentioning it here.

Tags: Apache, PHP

Discussion

Posted by jo on
add ? to RewriteRule ^blog\\.php$ blog-blog-title-14.html [L]

like

RewriteRule ^blog\\.php$ blog-blog-title-14.html? [L]

i think
Posted by Peter on
And the day is saved, thanks to Darryl. Thank you!
Posted by Berion on
It works!! Really, Thank you to share this very important information!!! (^o^)b
Posted by vlad on
Perfect solution, thanks Darryl Browne
Leave a Reply



(Your email will not be publicly displayed.)