JohnDiLaurie JohnDiLaurie - 2 months ago 6
Apache Configuration Question

Htaccess: Redirecting entire domain while redirecting certain user agents to other domain?

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?

Answer

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 .*$