{ Redirect (rewrite) via query string }

Redirect syntax:

RedirectMatch /^index.php?id=10 http://domain.com/page/

will simply ignore query strings. This means we will need to craft a Rewrite Rule in order to handle this:

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^index\.php$ http://domain.com/page/? [R=302,L]

The above RewriteCond looks at the query string, and matches the id to a regular expression that looks for any number of any length. Then we write the RewriteRule, which looks for the URL preceeding the query string. In this case it is index.php. It then redirects the user to a new page.

If I wanted to match a particular value in the query string, I would do this instead of using the regular expression:

RewriteCond %{QUERY_STRING} ^id=10$
RewriteRule ^index\.php$ http://domain.com/page/? [R=302,L]

NOTE: The ? in the RewriteRule tells the server to discard the current query string. Otherwise, you would end up redirecting the user to http://domain.com/page/?id=10.

Leave a Reply

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