在Nginx下進行wap和www訪問選擇
2024-08-30 12:23:35
供稿:網友
本文教大家解決手機端訪問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>
其實解決類似問題方法還是挺多的,這里就列舉這兩個簡答的辦法。