您的位置:首页 > 移动开发

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

2017-09-28 00:00 288 查看
摘要: 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Nginx