views:

1522

answers:

3

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.

+3  A: 

Redirect does only operate on the URL paths:

The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. […]

So the URL query (the part after the first ? up to the first # after) is not checked.

But you can use mod_rewrite to do that:

RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
RewriteRule ^nl/index\.php$ http://www.example.com/solutions/printsolutions.html [L,R=301]
RewriteCond %{QUERY_STRING} ^mID=24512&subID=0$
RewriteRule ^nl/index\.php$ http://www.example.com/support/koppeling-met-omgeving.html [L,R=301]
Gumbo
+1  A: 

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]
Martijn Heemels
The actual pattern for `RewriteRule` depends on if it’s used in the server configuration or in a .htaccess file and where the .htaccess file is located.
Gumbo
+1  A: 

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

Typo in Martjin'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 Martjin'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 Martjin'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.