Stop Apache mod_rewrite Appending Query String To Redirects

The Apache web server’s mod_rewrite module is a powerful tool for rewriting URLs. It can catch you off guard in certain situations. For example, if you’re matching a URL to rewrite or redirect to another, you may find that any query string is unexpectedly passed along, too.

Catching a Query String

Consider the following rewrite rule:

RewriteRule ^search\.php$ [L,R=301]

You would think that any requests to your old search page, search.php will now simply be redirected to your new URL at They will, but even though the regular expression contains a $ at the end, which you might expect to exclude a query string, it WILL still be apended. So your destination URL would be something along the lines of

Removing a Query String

Once you realise WHY that query string is getting appended (because mod_rewrite handles query strings using RewriteCond %{QUERY_STRING}) it’s straightforward to omit the query string from the redirect. You simply add a ? to the end of your destination URL.

So the rewrite rule now becomes this:

RewriteRule ^search\.php$ [L,R=301]

Useful Links:

Leave a Reply

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