描述:
根據用戶訪問的設備類型,相同的地址顯示不一樣的內容
好比,電腦上訪問http://192.168.0.100/shop/s888_a.html這個頁面顯示內容是」aaaaa」
而後用手機訪問http://192.168.0.100/shop/s888_a.html這個頁面內容是「bbbbb」,
但訪問的URL連接地址是同樣,相同的地址顯示不一樣的內容。php
需求1:html
電腦訪問: 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 (顯示這個頁面的內容)web
1htm 2it 3table |
電腦訪問: http://192.168.0.100/shop/s888_a.html ast 手機訪問: http://192.168.0.100/shop/s888_a.html (URL不變內容改變)配置 http://192.168.0.100/shop/article-256.html (顯示這個頁面的內容)rewrite |
需求2:tab
電腦訪問: 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; } |