How to redirect a URL with Query String apache htaccess

Redirect URL query string using htaccess

In this post we will learn how to redirect a URL with query string using RewriteRule directive in htaccess.

Query string represents URL part that is after ? character in URL.

A URL with query string looks like the following example URL:
http://example.com/file.php?q=foo&lang=en
As you can see the above example URL contains a querystring q=foo&lang=en with query keys and values.
Anything after ? in URL is part of QueryString.

Apache mod-rewrite has a nice method to test such URLs .
In mod rewrite you can match against your Query String URL using %{QUERY_STRING} in RewriteCond .
%{QUERY_STRING} variable contains the full URL querystring starting with the first query key ie q=foo&lang=en . You can use a regular expression based pattern to dynamically match against query key and value.

Redirect query string using htaccess

Let’s see a real world example of Query string URL redirection.
Assuming you have a perticular URL http://example.com/file.php?q=foo that you want to redirect to another URL say http://example.com/foo.html you will use a redirect logic in htaccess that first checks the QUERY_STRING and if it exists in URL then your RewriteRule will redirect it.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=foo$ [NC]
RewriteRule ^file.php$ http://example.com/foo.html? [R,L]

The rule above will redirect http://example.com/file.php?q=foo to http://example.com/foo.html

We use RewriteCond directive to test the URL querystring so that the rule can apply only to this specific QUERY_STRING.

If your query value q=foo is dynamic you could use regex catch all pattern .+ in the test string of RewriteCond.

Many new users often make a mistake when writing a rule for Query string URL. They write the full URL path including querystring in RewriteRule like the following:

RewriteEngine on
RewriteRule ^file.php?q=foo$ http://example.com/file.html? [L,R]

The rule above will never redirect /file.php?q=foo because you can not test or use QueryString like that.

The pattern of a RewriteRule matches the URL path only ie /file.php and the QUERYSTRING part isnt checked.

The correct way to redirect the above URL is :

RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=foo$ [NC]
RewriteRule ^file.php$ http://example.com/foo.htm? [R,L]

We use ? an empty question mark at the end of the destination path or URL to remove the old querystring. Otherwise if you don’t use ? then the above rule will append the old querystring q=foo to the new URL . Your new URL will then look something like http://example.com/file.html?q=foo .

You can not use Redirect or RedirectMatch to manipulate URL query string

Mod-rewrite beginners often use Redirect or RedirectMatch directives to test query string. I often see such questions being posted on stackoverflow and here is the one I answered a few months ago.

The reason why you can’t test query string that way is because those directives are designed to match against URL path only. You can’t test query string using Redirect and RedirectMatch.

Let’s see an example to better understand why you can’t use them:

Redirect /test.php?user=55 /

The example above looks as if it would redirect /test.php?user=55 to / but this does nothing instead You will get a 404 error if you try it.

This is because Redirect directive are designed just for URL path redirection.
So you need to remove the query string from its pattern in order for it to work.

Redirect /test.php /

Now it’s perfact! It will redirect both /test.php and /test.php?user=55 to /.

As I explained above, you can use RewriteCond along with RewriteRule to test the URL query string and then redirect based on the query string.

Hope this article was helpful.
Thanks for reading!

Leave a Reply

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