Ngixn根据手机端与电脑端设备相同地址显示不同页面内容

描述:
根据用户访问的设备类型,相同的地址显示不同的内容
比如,电脑上访问http://192.168.0.100/shop/s888_a.html这个页面显示内容是”aaaaa”
然后用手机访问http://192.168.0.100/shop/s888_a.html这个页面内容是“bbbbb”,
但访问的URL链接地址是一样,相同的地址显示不同的内容。

需求1:

电脑访问: http://192.168.0.100/shop/s888_a.html 手机访问: http://192.168.0.100/shop/s888_a.html (URL不变内容改变) http://192.168.0.100/shop/article-256.html (显示这个页面的内容)

1

2

3

电脑访问: http://192.168.0.100/shop/s888_a.html  

手机访问:  http://192.168.0.100/shop/s888_a.html (URL不变内容改变)

           http://192.168.0.100/shop/article-256.html (显示这个页面的内容)

 

需求2:

电脑访问: http://192.168.0.100/shop/10086.html 手机访问: http://192.168.0.100/shop/10086.html (URL不变内容改变) http://192.168.0.100/shop/article-512.html (显示这个页面的内容)

1

2

3

电脑访问: http://192.168.0.100/shop/10086.html  

手机访问:  http://192.168.0.100/shop/10086.html (URL不变内容改变)

           http://192.168.0.100/shop/article-512.html (显示这个页面的内容)

 

Nginx配置:

# 0 是PC端

set $tags '0';

# 1 是手机

if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {

set $tags '1';

}

if ( $uri ~ ^/shop/s888_a.html){

set $tags "${tags}1";

}

if ( $uri ~ ^/shop/10086.html){

set $tags "${tags}2";

}

# /shop/s888_a.html

if ( $tags = "11" ) {

rewrite . /article.php?mod=info&id=256 last;

}

# /shop/10086.html

if ( $tags = "12" ) {

rewrite . /article.php?mod=info&id=512 last;

}

# 这几条是原来Nginx的伪静态重写

rewrite ^/article-([0-9]+).html$    /article.php?mod=info&id=$1 last;

rewrite ^/shop/s([0-9]+)(\w*)\.html$ /goods.php?goods_id=$1&alias=$2&show=1 last;

rewrite ^/shop/([0-9]+).html$        /goods.php?goods_id=$1 last;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

# 0 是PC端

set $tags '0';

 

# 1 是手机

if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {

         set $tags '1';

}

 

if ( $uri ~ ^/shop/s888_a.html){

        set $tags "${tags}1";

}

 

if ( $uri ~ ^/shop/10086.html){

        set $tags "${tags}2";

}

 

# /shop/s888_a.html

if ( $tags = "11" ) {

           rewrite  .  /article.php?mod=info&id=256 last;

}

 

# /shop/10086.html

if ( $tags = "12" ) {

             rewrite  .  /article.php?mod=info&id=512 last;

}

 

 

# 这几条是原来Nginx的伪静态重写

rewrite  ^/article-([0-9]+).html$           /article.php?mod=info&id=$1  last;

rewrite  ^/shop/s([0-9]+)(\w*)\.html$  /goods.php?goods_id=$1&alias=$2&show=1 last;

rewrite ^/shop/([0-9]+).html$       /goods.php?goods_id=$1  last;

 

#那如果后过来呢,手机端访问内容不变,PC端访问内容改变。

# 0 是PC端 # 1 是手机端 if ( $tags = "01" ) { rewrite . /article.php?mod=info&id=256 last; }

1

2

3

4

5

# 0 是PC端

# 1 是手机端

if ( $tags = "01" ) {

           rewrite  .  /article.php?mod=info&id=256 last;

}

转载于:https://my.oschina.net/u/3084334/blog/1544821

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值