如何處理手機端和 PC 端跳轉不同頁面

PHPer技術棧發表於2019-08-02

NGINX 檢測跳轉

如果是手機端和pc端需要跳轉不同的頁面, 則需要在nginx.conf中的location / {}中加入如下程式碼:

if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
    rewrite ^.+ http://127.0.0.1:8080/mobile/$uri;
}

其中mobile為手機端的路徑標識, 可以自定義修改.

JavaScript 檢測跳轉

<script>
    //H5移動版自適應跳轉js
    var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mini", "mobile", "mobi", "mqqbrowser", "blackberry", "webos", "incognito", "webmate", "bada", "nokia", "symbian", "wp7", "wp8", "lg", "ucweb", "skyfire");
    var browser = navigator.userAgent.toLowerCase();
    var _tag = "{$_GET['tag']}";
    if(_tag != 'web'){
        for (var i = 0; i < mobileAgent.length; i++) {
            if (browser.indexOf(mobileAgent[i]) != -1) {
                window.location.href = 'http://www.xxx.com';
                break;
            }
        }
    }
</script>

相關文章