.htaccess 防止重定向到Google bot的移动版本

.htaccess 防止重定向到Google bot的移动版本,.htaccess,redirect,googlebot,.htaccess,Redirect,Googlebot,有一个网站mysite.com和子域名m.mysite.com。为了将移动设备从mysite.com重定向到m.mysite.com,我在htaccess中使用以下代码: <IfModule mod_rewrite.c> RewriteEngine on RewriteBase / # Check if this is the noredirect query string RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) # Se

有一个网站mysite.com和子域名m.mysite.com。为了将移动设备从mysite.com重定向到m.mysite.com,我在htaccess中使用以下代码:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:mysite.com]

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://m.mysite.com%{REQUEST_URI} [R,L]
</IfModule>

重新启动发动机
重写基/
#检查这是否是noredirect查询字符串
RewriteCond%{QUERY_STRING}(^ |&)m=0(&|$)
#设置一个cookie,并跳过下一条规则
重写规则^-[CO=mredir:0:mysite.com]
重写条件%{HTTP:x-wap-profile}!^$[或]
重写条件%{HTTP:Profile}!^$[或]
重写COND%{HTTP|u USER|u AGENT}“acs | alav | alca | amoi | audi | aste | avan | benq | bird | blac | blaz | brew | cell | cldc | cmd-”[NC,或]
重写第2%{HTTP|u USER|u AGENT}“dang|doco|eric|hipt|inno|ipaq|java|jigs | kddi | keji|leno | lg-c | lg d | lg g | lg g | lge-”[NC,或]
重写COND%{HTTP|u USER|u AGENT}“毛伊岛|马克索|米德普|米特斯| mmef |莫比|莫托|莫托| mwbp | nec |纽特|诺基| opwv”[NC,或]
重写第2%{HTTP|u USER|u AGENT}“palm | pana | pant | pdxg | phil | play | pluc | port | prox | qwap | sage | sams | sany”[NC,或]
重写第2%{HTTP|u USER|u AGENT}“sch-| sec-| send | seri | sgh-| shar | siem | smal | smar | sony | sph-| symb | t-mo”[NC,或]
重写cond%{HTTP|u USER|u AGENT}“teli | tim | tosh | tsm | upg1 | upsi | vk-v | voda | w3cs | wap | wapa | wapi”[NC,或]
重写cond%{HTTP|u USER|u AGENT}“wapp | wapr | webc | winw | winw | xda | xda-”[NC,或]
重写cond%{HTTP_USER_AGENT}“up.browser | up.link | windowssce | iemobile | mini | mmp”[NC,或]
重写cond%{HTTP|u USER|u AGENT}“symbian | midp | wap | phone | pocket | mobile | pda | psp”[NC]
RewriteCond%{HTTP\u USER\u AGENT}!macintosh[NC]
#检查我们是否已经在移动站点上
重写cond%{HTTP_HOST}^m\。
#无法在同一请求中读取和写入cookie,必须重复条件
重写cond%{QUERY_STRING}!(^ |&)m=0(&|$)
#检查以确保我们之前没有设置cookie
重写cond%{HTTP_COOKIE}!^*mredir=0.*$[NC]
#现在重定向到移动站点
重写规则^http://m.mysite.com%{REQUEST_URI}[R,L]
现在的问题是,托管在mysite.com但不在m.mysite.com的图片,以及插入到电子邮件中的图片,没有加载到Gmail中。可能是因为谷歌机器人被重定向到m.mysite.com。临时决定是在m.mysite.com上为电子邮件托管图像

RewriteCond%{HTTP\u USER\u AGENT}之后,我为RewriteCond添加了另一个条件!macintosh[NC]
RewriteCond%{HTTP\u USER\u AGENT}^谷歌机器人[NC]
但这似乎不起作用。如果图像不在m.mysite.com中,则无法在Gmail中访问

UPD:

这是解决我问题的字符串:
RewriteCond%{HTTP\u USER\u AGENT}^macintosh |谷歌图像代理[NC]


感谢阿披舍克•古尔贾尔的帮助。

尝试规则中的以下条件,注释现有的类似测试条件

RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !^macintosh|Googlebot [NC]

不工作:(可能为电子邮件抓取图像的bot有不同的名称,而不是Googlebot。请在此处查看代理名称。当将
Googlebot
更改为
Google
时,我使其正常工作。呃……是否有任何移动浏览器用户代理字符串包含“Google”?:DEureka!查看apache日志并找到想要的bot:
GoogleImageProxy
。此
Rewri。)第二个%{HTTP\u USER\u AGENT}!^googlebot[NC]
不起作用,可能是因为googlebot的用户代理并不总是以“googlebot”开头,而是包含它。因此,您只需通过以下方式从重写中删除“^:
重写第二个%{HTTP\u USER\u AGENT}!googlebot[NC]