Tag Archives: RewriteRule

RewriteRule: How to avoid passing query string when you redirect.

Today I ran into an issue which I could not figure out for quite a while. I was trying to redirect a url to another url using RewriteRule. It was supposed to be a pretty straightforward redirect which made it even more annoying than complicated ones. Hopefully somebody is able to explain what I am missing here and if my solution is a good solution or not.

I needed to redirect a url:  http://www.example.com/testpage.php?foo=bar to http://www.example.com/

So I added this to .htaccess file:

RewriteRule ^testpage\.php / [R=301,L]

This did do a 301 redirect as I wanted but query string passed so my redirected url looked like:  http://www.example.com/?foo=bar

Obviously this is not what I wanted so in order for me to fix it, I had to take a rather lame approach.  My new redirect is:

RewriteRule ^testpage\.php /? [R=301,L]

The only difference is the “?” after / for target url.  I am just curious to know why query-string was passed.  I have not used rewrites for long time but I do not remember this behavior.