Nginx to achieve mobile phone WAP version and PC version of the website Access choice

Source: Internet
Author: User
Tags lenovo

Requirements: Mobile phone tablet access to the site, jump to the WAP page of the mobile phone, computer access to the website, jump to PC PC page.


There are several kinds of solutions, roughly divided into two categories, one is implemented on the server, and the other is through the page JS implementation. But can be implemented on the server, try not to implement on the page. Search from the Internet for a few code first labeled below.


Method One: Nginx according to Http_user_agent judgment, in the nginx.conf configuration:


Code One:

server {
Listen 80;
server_name localhost;
#charset Koi8-r;
#access_log Logs/host.access.log Main;
Location/{
root/usr/share/nginx/html;

#这里大小写敏感, ~* ignores the case.
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;
}



Code two (not tested):

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;
}

Method two: On each page, through the Baidu Uaredirect.js to achieve


<script src= "Http://siteapp.baidu.com/static/webappservice/uaredirect.js" type= "Text/javascript" ></ Script><script type= "Text/javascript" >uaredirect ("http://www. Mobile website address. com");</script>


There are a number of workarounds and codes, and here are just a few of the more convenient and simple ones.


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

This article is from the "Deadwood Self-Carving" blog, please be sure to keep this source http://321602.blog.51cto.com/311602/1718959

Nginx to achieve mobile phone WAP version and PC version of the website Access choice

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.