Redirect directive ignored in .htaccess when using URL parameters

Posted on

Redirect directive ignored in .htaccess when using URL parameters – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, .htaccess, redirect, , .

I have a default .htaccess file that comes with drupal. In order to redirect externally linked pages from the old website to the new one I usually use statements like

Redirect /foo http://www.example.com/bar

This works as expected.

But the statement is totally ignored when looking like

Redirect /index.php?x=y http://www.example.com

and the request runs through the mod_rewrite rules from Drupal at the end of the .htaccess file.

Do you know what is the problem?

Solution :

Problem

Redirect does not match URL parameters, it will only match the PATH of a url.

Solution

You CAN match the QueryString variable with RewriteCond and RewriteRule. Here is a working (but untested) snippet that you can try:

RewriteCond %{QUERY_STRING} x=y
RewriteRule ^index.php http://www.example.com [R=301]

You should note a few things about this:

  1. The RewriteCond line is really a regex (regular expression), so you will have to escape any special characters you add to it (such as punctuation), and the first ? is omitted, but any further URL parameters with have to include the & (RewriteCond %{QUERY_STRING} x=y&a=b).
  2. This method is equvilent to a Redirect, because I specified the HTTP status code (a HTTP 301 Redirect, which is permanent).

Credits

Leave a Reply

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