PDA

View Full Version : How do I redirect an existing website with htaccess without redirecting major robots?


zylstra
Mar 29, 2012, 04:02 PM
To temporarily test sales on a newly redesigned site (that resides on sub.domain.com) without disrupting my existing search engine rankings I would like to redirect all visitors that aren't a major crawler to the new subdomain. The existing site contains the product ID (SKU) at the end of the product page URLs after an underline character, "_". I would like to redirect visitors that enter on a product page to the newly redesigned product page with the structure, http://sub.domain.com/toy/sku-[Product ID]?a=test . All others (including visitors that enter on category pages) I would like to redirect to the home page. Is this a good .htaccess file for this task?



RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} !^(AltaVista|Googlebot|msnbot|Slurp)
RewriteRule ^_(.*)$ http://sub.domain.com/toy/sku-$1?a=test [R=302,L]

RewriteCond %{HTTP_USER_AGENT} !^(AltaVista|Googlebot|msnbot|Slurp)
RewriteRule (.*) http://sub.domain.com/ [R=302,L]

rpray2007
Mar 30, 2012, 09:58 AM
I think it should work based on my understanding of regex and htaccess rules. My only question is if you have the list of the bots correct - maybe look at your access logs to make sure you got the correct user-agent names. They look right to me...

The order of these rules is important too - the catch-all rule has to come after the restrictive one. And you are doing this by providing the 'L' directive in the redirect which tells to stop redirecting if this rule fires.

You can look at this link to get more details.
mod_rewrite - Apache HTTP Server (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)

But you've probably read it ;-)

zylstra
Mar 30, 2012, 04:52 PM
I had a couple mistakes. The fixed code is:


RewriteCond %{HTTP_USER_AGENT} !(AltaVista|Googlebot|msnbot|Slurp)
RewriteRule _(.*)$ http://sub.domain.com/toy/sku-$1 [R=302,L]

RewriteCond %{HTTP_USER_AGENT} !(AltaVista|Googlebot|msnbot|Slurp)
RewriteRule (.*) http://sub.domain.com/ [R=302,L]