nginx 移动端和pc端自动跳转

https://www.jianshu.com/p/ea482004f32f

场景
域名描述
pc端www.one.com用于pc端访问官网
移动端m.one.com用于移动端访问

现在的需求是这样,在pc端访问www.one.comm.one.com都跳转到www.one.com
而在移动端访问www.one.comm.one.com都跳转到m.one.com

参考,github上的这篇文章很详细,但是比较复杂,很多场景我们用不到,所以参考这个,我修改如下。

pc端:www.one.com

  server {
      listen       80;
      server_name  www.one.com;

      #charset koi8-r;
      #access_log  logs/host.access.log  main;
    # 下面根据user_agent可以获取
     if ($http_host !~ "^www.one.cn$") { rewrite ^(.*) http://www.one.cn$1 permanent; } if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { rewrite ^(.*) http://m.one.com$1 permanent; } location / { root /home/build/rampage-home-front/dist/html; index index.html index.htm; } } 

作用部分代码如下:

 if ($http_host !~ "^www.one.cn$") {
  rewrite  ^(.*)    http://www.one.cn$1 permanent;
 }
 if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { rewrite ^(.*) http://m.one.com$1 permanent; } 

移动端:m.one.com

  server {
      listen       80;
      server_name  m.one.cn;

      #charset koi8-r;
      #access_log  logs/host.access.log  main;
    #非移动端跳转到 www.one.com if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { rewrite ^(.*) http://www.one.com$1 permanent; } location / { root /home/build/rampage-mobile-front/dist; index index.html index.htm; } } 

作用部分代码如下:

 if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) { rewrite ^(.*) http://www.one.com$1 permanent; } 

至此完成了相关配置



作者:阿亮私语
链接:https://www.jianshu.com/p/ea482004f32f
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值