chaihongjun.me

配置nginx指引百度移动搜索引擎访问移动页面

最近在观察网站优化方面的时候突然有一个想法,之前百度发布消息,新版本的百度蜘蛛UA升级。然后列出了新版本移动UA和PC端UA:

#新移动UA
Mozilla/5.0 (Linux;u;Android 4.2.2;zh-cn;) AppleWebKit/534.46 (KHTML,like Gecko) Version/5.1 Mobile Safari/10600.6.3 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html) 
Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)

并且生命了新的正确的识别Baiduspider移动UA的方法:

  1. 通过关键词"Android"或者"Mobile"

  2. 通过关键词"Baiduspider/2.0"

可能存在这么两个场景,百度的移动端蜘蛛在访问PC端网站的页面或者百度的PC端蜘蛛在访问移动端的页面。显然,这两者情况都不是我们愿意看到的。为了杜绝这样的情况发生,我们在服务器端的nginx做一些配置,当百度PC端的蜘蛛在访问我们的移动页面的时候指引前往PC页面,相应的当百度的移动端蜘蛛访问我们的PC端页面的时候,也指引它前往我们的移动页面。归根结底,属于什么端的蜘蛛让它就去哪个端的页面。

# 如果是移动端蜘蛛,则让它访问移动页面
# spider_pc_2_mobile.conf 
set $spider 0; 
if ($http_user_agent ~* "Android"){
  set $spider "${spider}1";
}
if ($http_user_agent ~* "Baiduspider/2.0"){
 set $spider "${spider}2";
}
if ($spider = "012") {
           return 301 https://m.yourdomain.com$request_uri;
  }
# 如果是PC端蜘蛛,则让它访问PC页面
# spider_mobile_2_pc.conf 
set $spider 0;
if ($http_user_agent !~ "Andriod"){
 set $spider "${spider}1";
}
if ($http_user_agent !~ "Mobile"){
 set $spider "${spider}2";
}
if ($http_user_agent ~* "Baiduspider/2.0"){
 set $spider "${spider}3";
}
if ($spider = "0123") {
           return 301 https://www.yourdomain.com$request_uri;
  }

spider_pc_2_mobile.conf这个文件需要在PC端的网站配置中引入,spider_mobile_2_pc.conf需要在移动端网站配置文件中引入,而且特别注意,这俩个文件都要尽早引入,以免造成过多重定向,另外,无论是在http还是https段内都应当引入,避免过多重定向。还有就是PC和移动端的页面有一一对应。


知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。作者:柴宏俊»