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.

7 thoughts on “RewriteRule: How to avoid passing query string when you redirect.

  1. Olly

    you’re doing the right thing (apart from using RedirectMatch, as Brian wrote) with the ‘?’.
    So its supposed to pass the querystring per default, if no ‘?’ is given.

    That information is somehow hidden in the documentation:
    http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

    I copied that part out of there:

    Note: Query String

    The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.

  2. moncler

    I wanted to put you the tiny remark to thank you so much the moment again regarding the precious ideas you’ve discussed on this website. It’s so unbelievably open-handed with you in giving freely just what a lot of folks would have distributed as an e-book in making some bucks on their own, especially since you could have tried it in case you decided. Those basics also worked as the fantastic way to comprehend some people have the identical zeal similar to mine to find out good deal more related to this problem. Certainly there are several more pleasurable times ahead for folks who look into your site.

  3. chrome hearts

    Thank you for all of your work on this web site. Kim takes pleasure in making time for research and it’s really easy to understand why. Many of us notice all of the lively manner you present valuable guidelines by means of the website and even inspire response from other individuals on this situation and our favorite daughter is certainly learning a whole lot. Take advantage of the remaining portion of the year. Your performing a very good job.

  4. nmd uk

    My wife and i were quite joyful when Louis could round up his studies by way of the precious recommendations he came across when using the weblog. It is now and again perplexing just to be giving out concepts which usually people might have been making money from. We fully understand we’ve got the writer to appreciate because of that. Most of the explanations you have made, the easy site navigation, the relationships you can aid to promote – it is everything wonderful, and it’s really facilitating our son and us believe that this matter is fun, which is incredibly important. Thank you for all!

  5. balenciaga sneakers

    I would like to express my affection for your kind-heartedness giving support to persons who really need guidance on this particular study. Your very own dedication to passing the solution up and down has been exceptionally powerful and have usually encouraged ladies much like me to achieve their desired goals. Your entire informative guideline denotes much to me and extremely more to my fellow workers. Warm regards; from everyone of us.

Leave a Reply

Your email address will not be published. Required fields are marked *