需求:手机平板访问网站,跳转到手机WAP页面,电脑访问网站,跳转到电脑PC页面。


解决方法有好几种,大致分成两类,一是在服务器上实现,二是通过页面js实现。但能在服务器上实现的,就尽量不要在页面上实现。从网上搜索来几个代码先标注下。


方法一:nginx根据http_user_agent判断,在nginx.conf配置:


代码一:

 server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        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)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera))"  )
      {

        #wap版目录
        root /usr/share/nginx/html/mobile;
        }    

        index index.php index.html index.htm;
}



代码二(未测试):

 server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

location = / {
root /usr/share/nginx/html;
if ($http_user_agent ~* "Nokia") { rewrite . /index.html break; }
if ($http_user_agent ~* "Mobile") { rewrite . /index.html break; }
if ($http_user_agent ~* "SAMSUNG") { rewrite . /index.html break; } }
if ($http_user_agent ~* "SonyEricsson") { rewrite . /index.html break; }
if ($http_user_agent ~* "MOT") { rewrite . /index.html break; } }
if ($http_user_agent ~* "BlackBerry") { rewrite . /index.html break; }
if ($http_user_agent ~* "LG") { rewrite . /index.html break; } }
if ($http_user_agent ~* "HTC") { rewrite . /index.html break; }
if ($http_user_agent ~* "J2ME") { rewrite . /index.html break; }
if ($http_user_agent ~* "Opera Mini") { rewrite . /index.html break; } }
index index.php index.html;
}

方法二:在每个页面,通过百度的uaredirect.js来实现


<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://www.手机网站的地址.com");</script>


解决方法和代码有很多种,这里只列出几个比较方便简单的。


--------------------------------------------------------------------------------