在Nginx下进行wap和www访问选择
栏目:nginx问题汇总
时间:2019-06-24 17:17
本文教大家解决手机端访问www网站时切换到wap网页,同时pc机访问wap时切换到www端问题的解决办法。
方法一:nginx根据http_user_agent判断,在nginx.conf配置:
代码一:
server{
listen80;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/{
root/usr/share/nginx/html;
#这里大小写敏感,~*无视大小写。
if($http_user_agent~"((MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT)|(SonyEricsson)|(NEC)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC)|(SED)|(EMOL)|(INNO55)|(ZTE)|(iPhone)|(Android)|(WindowsCE)|(Wget)|(Java)|(curl)|(Opera))")
{
#wap版目录
root/usr/share/nginx/html/mobile;
}
indexindex.phpindex.htmlindex.htm;
}
代码二:
server{
listen80;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location=/{
root/usr/share/nginx/html;
if($http_user_agent~*"Nokia"){rewrite./index.htmlbreak;}
if($http_user_agent~*"Mobile"){rewrite./index.htmlbreak;}
if($http_user_agent~*"SAMSUNG"){rewrite./index.htmlbreak;}}
if($http_user_agent~*"SonyEricsson"){rewrite./index.htmlbreak;}
if($http_user_agent~*"MOT"){rewrite./index.htmlbreak;}}
if($http_user_agent~*"BlackBerry"){rewrite./index.htmlbreak;}
if($http_user_agent~*"LG"){rewrite./index.htmlbreak;}}
if($http_user_agent~*"HTC"){rewrite./index.htmlbreak;}
if($http_user_agent~*"J2ME"){rewrite./index.htmlbreak;}
if($http_user_agent~*"OperaMini"){rewrite./index.htmlbreak;}}
indexindex.phpindex.html;
}
方法二:在每个页面,通过百度的uaredirect.js来实现
<scriptsrc="http://siteapp.baidu.com/static/webappservice/uaredirect.js"type="text/javascript"></script><scripttype="text/javascript">uaredirect("http://www.手机网站的地址.com");</script>
其实解决类似问题方法还是挺多的,这里就列举这两个简答的办法。