0

as the title states, I would like to redirect an entire domain to a single URL, while redirecting certain user agents to a different domain. It's important that all redirects are initiated by htaccess. That's my code so far:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com
RewriteRule (.*) http://newdomain.com [R=301,L]

RewriteCond %{REQUEST_URI} !/robots.txt$
RewriteCond %{HTTP_USER_AGENT} ^.*YandexBot.*$ [NC]
RewriteCond %{HTTP_USER_AGENT} ^.*BaiduSpider.*$ [NC]
RewriteRule ^.*.* http://otherdomain.com [L]

Any help would be greatly appreciated!

Edit: My code looks as follows now:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^olddomain\.de [NC]
RewriteCond %{REQUEST_URI} !/robots\.txt$
RewriteCond %{HTTP_USER_AGENT} ^.*BLEXBot.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*BlackWidow.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*Nutch.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*Jetbot.*$ [NC,OR]
# [...] (a lot more user agents)
RewriteCond %{HTTP_USER_AGENT} ^.*gigabot.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*BlekkoBot.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*Nexus 10.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*YandexBot.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*BaiduSpider.*$ [NC]
RewriteRule ^ http://www.otherdomain.de/ [L]

RewriteCond %{HTTP_HOST} ^olddomain\.de [NC]
RewriteRule ^ https://newdomain.de/ [R=301,L]

Server is still returning a 500 Internal Server error, when I use the above htaccess file. What does my robots.txt have to look like? Haven't modified it so far. Is it necessary?

1 Answers1

0

You can use:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteCond %{REQUEST_URI} !/robots\.txt$
RewriteCond %{HTTP_USER_AGENT} YandexBot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} BaiduSpider [NC]
RewriteRule ^ http://otherdomain.com [L]

RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^ http://newdomain.com [R=301,L]

With robot redirection before domain. And OR after each new user agent.
No need to use ^.* and .*$

Croises
  • 18,570
  • 4
  • 30
  • 47
  • Thanks for the rapid help, Croises! Just edited my question. The server is still returning a 500 Internal Server error when using the above htaccess file. See question for more details. Thanks! – JohnDiLaurie Sep 10 '16 at 20:10
  • Same problem without a lot more user agents ? – Croises Sep 10 '16 at 23:44
  • Resolved it, your version is working perfectly, thanks! Just didn't have a proper robots.txt in place... – JohnDiLaurie Sep 11 '16 at 10:56