• 懒鸟飞:帝国cms模板专业分享平台,新域名:lanniaofei.com

当电脑访问网站时跳转到404页面,手机访问时正常显示页面

以下是实现电脑端访问跳转到404页面,手机浏览器访问正常显示网页的几种方法:

方法一:使用Nginx配置
在Nginx服务器配置文件中,可以通过检测`$http_user_agent`来区分设备类型,实现跳转。

server {
    listen 80;
    server_name example.com;

    # 检测是否为电脑端访问
    if ($http_user_agent !~* "(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino") {
        return 404;
    }

    location / {
        root /path/to/your/website;
        index index.html index.htm;
    }
}


方法二:使用Apache配置
在Apache服务器的`.htaccess`文件中,可以通过`SetEnvIf`指令检测用户代理,并结合`ErrorDocument`指令实现跳转。

SetEnvIfNoCase User-Agent "android|bbd+|meego.+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino" MOBILE

ErrorDocument 404 /404.html

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{ENV:MOBILE} ^$
    RewriteRule ^ - [R=404,L]
</IfModule>


方法三:使用JavaScript检测
在网页的HTML文件中,通过JavaScript检测用户代理,实现跳转。

<script type="text/javascript">
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    if (flag) {
        window.location.href = "/404.html"; // 电脑端跳转到404页面
    }
</script>


方法四:使用PHP检测
在PHP代码中,通过检测`$_SERVER['HTTP_USER_AGENT']`来实现跳转。

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (!preg_match('/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i', $useragent)) {
    header('Location: /404.html'); // 电脑端跳转到404页面
}
?>
 
 
QQ在线咨询
QQ客服
淘宝官网