nginx配置不同设备访问不同地址

不同设备访问不同的网页地址

前端处理(笨办法)

通过navigator.userAgent就可以获取到用户的设备从而进行跳转

// 获取用户设备字符串
var userAgent = navigator.userAgent;

// 检测是否是移动设备
var isMobile = /Mobi|Android/i.test(userAgent);

// 定义目标URL
var desktopUrl = 'https://www.xxxxxxx.com/desktop';
var mobileUrl = 'https://www.xxxxxxx.com/mobile';

// 根据设备类型重定向到相应的URL
if (isMobile) {
    window.location.href = mobileUrl;
} else {
    window.location.href = desktopUrl;
}

nginx配置(推荐)

使用Nginx$http_user_agent变量和条件指令来实现

server {
	......
    location / {
        # 检查用户代理是否包含移动设备相关的关键词
        if ($http_user_agent ~* "Mobi|Android") {
            # 如果是移动设备,重定向到移动版网页
            return 302 https://m.xxxxxxx.com$request_uri;
        }
    }
}

或者域名不变

http {
	......
    map $http_user_agent $isMobile {
        default 0;
        ~*Mobi|Android 1;
    }
    server {
    	......
        location / {
        	# 默认访问PC
        	root /www/PC/....
            # 检查$isMobile变量是否为1,即是否是移动设备
            if ($isMobile = 1) {
                # 如果是移动设备,重定向到移动版网页
               	root /www/Mobi/....
            }
        }
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值