Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Redirect 301 fails when using GET parameters, such as ?blah=

I've built a new PHP site for a customer and want to redirect the top ranking Google results from the old site structure to the new one.

I've put several dozen Redirect 301's in a .htaccess in the documentroot, and while some work fine I'm having issues with a bunch of others.

This works fine:

Redirect 301 /nl/flash/banner_new.swf http://www.example.com/actueel/nieuws.html?action=show&f_id=152

This doesn't work! (leading to a 404 since the redirect is simply skipped):

Redirect 301 /nl/index.php?mID=24511&subID=0 http://www.example.com/solutions/printsolutions.html
Redirect 301 /nl/index.php?mID=24512&subID=0 http://www.example.com/support/koppeling-met-omgeving.html

The redirects are mixed in the .htaccess file, and only the redirects with GET parameters appear to fail.

Is there a workaround? Ignoring the failing redirects is not an option to the customer. Thanks for your thoughts.

like image 418
Martijn Heemels Avatar asked Aug 11 '09 14:08

Martijn Heemels


People also ask

Why are my 301 redirects not working?

The reasons for 301 redirect not working are much more well-defined among WordPress sites. One of the main causes is because you have added the rewrite rules on both the cPanel “Redirects” tool and from your WordPress plugin.

Should I enable 301 .htaccess redirect?

Because the WordPress 301 redirect is not always reliable, we recommend issuing the 301 redirect via your . htaccess file. Another benefit is that the . htaccess redirect is slightly faster than redirecting via PHP, because it is loaded even before the rest of the page.

What is Apache 301 redirect?

Apache: HTTP to HTTPS redirect: How it works Also for this case, you can work with an Apache 301 redirect that automatically redirects visitors to the HTTPS version of your pages.


2 Answers

While Gumbo's answer's reasoning was correct, I could not get his RewriteRule to work.

Adding another RewriteCond did it. The following was tested and works fine.

RewriteCond %{REQUEST_URI} /nl/index.php$
RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html [L,R=301]
like image 58
Martijn Heemels Avatar answered Sep 24 '22 23:09

Martijn Heemels


Agreeing with both Gumbo's and Martijn's answers ... but:

Typo in Martijn's, there should be be "^" to start the regular expression for the REQUEST_URI condition:

RewriteCond %{REQUEST_URI} ^/nl/index.php$

I too could only get Martijn's, not Gumbo's, to work where my .htaccess file was.

Also, if you don't want the parameter string to be passed on with the rewrite, you should add a "?" on the end of the URL:

RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html? [L,R=301]

Otherwise, following Martijn's code, it reads "if your URL is /nl/index.php?mID=24511&subID=0 then redirect to http://www.example.com/solutions/printsolutions.html?mID=24511&subID=0 with a 301 Permanent redirect header and don't process more rules on this URL"

This may or may not be what you want, and to be fair as a general rule if parameters are not understood they will simply be ignored without doing any harm, so it probably won't matter. However if you're wanting to redirect a human to a new page and want "pretty URLs" then stripping off the parameter string is preferable, so stick the "?" on the end of the destination URL.

like image 32
user204529 Avatar answered Sep 24 '22 23:09

user204529