I need to permanently redirect all visitors to an specific domain except search bots (Google, Yahoo, MSN, etc.), How should I do it using htaccess?
Asked
Active
Viewed 2,105 times
1
-
Search for http://stackoverflow.com/search?q=[.htaccess]+bot or http://stackoverflow.com/search?q=[mod-rewrite]+bot and you will find some answers, e.g. http://stackoverflow.com/q/18076099/1741542, http://stackoverflow.com/q/12904684/1741542 – Olaf Dietsche Nov 23 '14 at 10:51
-
@OlafDietsche: I found this: http://stackoverflow.com/questions/12904684/htaccess-301-redirection-for-the-bots ... but I don't know how to edit rules to match my case. I think that is vice versa. – John Smith Nov 23 '14 at 10:55
2 Answers
1
You can reverse the rules using negation in RewriteCond
like this:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(110\.174\.129\.147|203\.217\.17\.162)
RewriteCond %{HTTP_USER_AGENT} !(Googlebot|msnbot|Surp) [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NE]

anubhava
- 761,203
- 64
- 569
- 643
-
Thanks, Can you please tell me how to do it with IP addresses? I mean I need to allow some websites just like the search bots and don't redirect them. How can I allow IP or Domain? – John Smith Nov 23 '14 at 11:44
-
Thank you very much. Just another question. Can I join the first part of conditions with OR ? I mean something like this: %{HTTP_CLIENT_IP|REMOTE_ADDR|...} – John Smith Nov 23 '14 at 11:54
-
0
To allow all bots, you can implement a do nothing rule, e.g.
RewriteCond %{HTTP_USER_AGENT} (bot|spider) [NC]
RewriteRule ^ - [L]
This rule triggers for all visitors, which have either bot
or spider
in their user agent string. You can be more specific of course, if you want only a few bots like Google, Yahoo and Bing. When the condition is met, nothing else happens.
To redirect everybody else to another domain, just append a simple redirect rule
RewriteRule ^ http://www.example.com [R,L]
When everything works as you expect, you can change R
to R=301
.
Never test with 301
enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
- 1
- 1

Olaf Dietsche
- 72,253
- 8
- 102
- 198