防止重定向到 Google 机器人的移动版本

Prevent redirection to mobile version for Google bot

有一个站点 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>

现在的问题是托管在 mysite.com 而不是托管在 m.mysite.com 并插入到电子邮件中的图像未加载到 Gmail 中。可能是因为 Google 机器人被重定向到 m.mysite.com。临时决定将图片托管在 m.mysite.com.

我在 RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] 之后向 RewriteCond 添加了另一个条件: RewriteCond %{HTTP_USER_AGENT} !^googlebot [NC] 但这似乎不起作用。如果图片不在 m.mysite.com 中,则无法在 Gmail 中访问。

UPD:

这是解决我问题的字符串:RewriteCond %{HTTP_USER_AGENT} !^macintosh|GoogleImageProxy [NC]

感谢 Abhishek gurjar 的帮助。

在您的规则中尝试以下条件,注释掉现有的类似条件以进行测试。

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