【问题标题】:Htaccess: Redirecting entire domain while redirecting certain user agents to other domain?Htaccess:重定向整个域,同时将某些用户代理重定向到其他域?
【发布时间】:2017-01-18 15:52:10
【问题描述】:

正如标题所述,我想将整个域重定向到单个 URL,同时将某些用户代理重定向到不同的域。 重要的是所有重定向都由 htaccess 发起。 到目前为止,这是我的代码:

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]

任何帮助将不胜感激!

编辑:我的代码现在如下所示:

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]

当我使用上述 htaccess 文件时,服务器仍然返回 500 Internal Server 错误。 我的 robots.txt 应该是什么样子?到现在还没修改。有必要吗?

【问题讨论】:

    标签: apache .htaccess directive


    【解决方案1】:

    你可以使用:

    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]
    

    在域之前使用机器人重定向。和OR 在每个新用户代理之后。
    无需使用^.*.*$

    【讨论】:

    • 感谢您的快速帮助,Croises!刚刚编辑了我的问题。使用上述 htaccess 文件时,服务器仍然返回 500 Internal Server 错误。有关更多详细信息,请参阅问题。谢谢!
    • 没有更多用户代理的同样问题?
    • 已解决,您的版本运行良好,谢谢!只是没有适当的 robots.txt...
    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 2011-08-27
    相关资源
    最近更新 更多