ChrisB
ChrisB

Reputation: 11

.htaccess redirect 301 not working with arguments

We recently rebuilt a client's website. They have found Google is listing some old links. We're trying to redirect these link just to the home page.

http://www.joiningtech.com/index.html?GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA http://www.joiningtech.com/index.html?GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ

I have tried several ways to get these two redirects to work correctly by using the .htaccess file.

Method #1

redirect 301 http://www.joiningtech.com/index.html?GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA http://www.joiningtech.com/
redirect 301 http://www.joiningtech.com/index.html?GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ http://www.joiningtech.com/

Method #2

RewriteCond %{QUERY_STRING} ^([^&]&)*GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA(&|$)
RewriteRule ^/index\.php$ http://www.joiningtech.com/? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]&)*GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ(&|$)
RewriteRule ^/index\.php$ http://www.joiningtech.com/? [L,R=301]

I found these methods in other places in stackoverflow. I'm wondering if I've gotten something wrong and would appreciate any assistance!

Thanks, Chris

Upvotes: 1

Views: 971

Answers (2)

ChrisB
ChrisB

Reputation: 11

OK, thanks Kamil's help and some trial and error, I found the answer. This site is a WordPress site. The .htaccess file had the following in this order:

# suExec for PHP
Action application/x-pair-sphp /cgi-bin/php5.cgi
AddType application/x-pair-sphp .php
#Video MIME Types:
AddType video/m4v .m4v
AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>
redirect 301 /pages/laser.html http://www.joiningtech.com/pages/welding.html
redirect 301 /index_nosound.html http://www.joiningtech.com/
RewriteEngine On
RewriteCond %{REQUEST_URI} =/index.html [NC]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA [OR]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ
RewriteRule .* http://www.joiningtech.com/? [L,R=301]

I'm guessing that the WordPress rewriting code and the later codes were conflicting. So I reordered the WordPress rewriting code to below the new redirects.

# suExec for PHP
Action application/x-pair-sphp /cgi-bin/php5.cgi
AddType application/x-pair-sphp .php
#Video MIME Types:
AddType video/m4v .m4v
AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm

</IfModule>
redirect 301 /pages/laser.html http://www.joiningtech.com/pages/welding.html
redirect 301 /index_nosound.html http://www.joiningtech.com/
RewriteEngine On
RewriteCond %{REQUEST_URI} =/index.html [NC]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA [OR]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ
RewriteRule .* http://www.joiningtech.com/? [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

This now works. I still need to know if it's ok to have two instances of "RewriteEngine On".

Upvotes: 0

Kamil Šrot
Kamil Šrot

Reputation: 2221

Use lexicographical comparition, it's much clearer than regular expressions when you don't need them.

RewriteEngine On
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA [OR]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ
RewriteRule ^index.html$ http://www.joiningtech.com/? [L,R=301]

Your first method (using Redirect) doesn't work as you specify full URL as the first argument but it expects only the pathname part of the URL. Following will work as well:

redirect 301 /index.html?GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA http://www.joiningtech.com/
redirect 301 /index.html?GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ http://www.joiningtech.com/

The third way how to do it using mod_rewrite is:

RewriteEngine On
RewriteCond %{REQUEST_URI} =/index.html [NC]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CNbT06iehLQCFQWe4AodejAAkA [OR]
RewriteCond %{QUERY_STRING} =GAW+CAM2&gclid=CMncjs-ehLQCFdKd4Aod52AAAQ
RewriteRule .* http://www.joiningtech.com/? [L,R=301]

Upvotes: 1

Related Questions