天高云淡的博客

Good Luck To You!

nginx rewrite 例子

链接地址规则
H5-URL地址     PC-URL地址
mshop.web.cn     shop.web.cn
mshop.web.cn/main.html     shop.web.cn/main.html
mshop.web.cn/gold     shop.web.cn/gold.html
mshop.web.cn/premium     shop.web.cn/premium.html
mshop.web.cn/vehicles     shop.web.cn/vehicles.html
mshop.web.cn/boosters     shop.web.cn/boosters.html
mshop.web.cn/specials     shop.web.cn/specials.html
mshop.web.cn/detail/ps_188     shop.web.cn/detail.html?goods_id=ps_188
要求:
pc访问H5地址的的时候 跳到对应的pc地址
H5访问pc地址的时候  跳到对应的H5地址
mshop.web.cn

location / {
    set $flag -1;
    set $url $http_host$request_uri;
    if ($http_user_agent !~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") {
        set $flag 0;
        }
    if ($url ~* "(?=mshop.web.cn)"){ set $flag "${flag}0"; }
    if ($url ~* "(?=main.html)"){ set $flag "${flag}1"; }
    if ($url ~* "(?=gold)"){ set $flag "${flag}2"; }
    if ($url ~* "(?=premium)"){ set $flag "${flag}3"; }
    if ($url ~* "(?=vehicles)"){ set $flag "${flag}4"; }
    if ($url ~* "(?=boosters)"){ set $flag "${flag}5"; }
    if ($url ~* "(?=specials)"){ set $flag "${flag}6"; }
    if ($url ~* "(?=detail/ps_)"){ set $flag "${flag}7"; }
    if ($flag = "001"){ rewrite "^/(.*)$" http://shop.web.cn/main.html; break;}
    if ($flag = "002"){ rewrite "^/(.*)$" http://shop.web.cn/gold.html; break;}
    if ($flag = "003"){ rewrite "^/(.*)$" http://shop.web.cn/premium.html; break;}
    if ($flag = "004"){ rewrite "^/(.*)$" http://shop.web.cn/vehicles.html; break;}
    if ($flag = "005"){ rewrite "^/(.*)$" http://shop.web.cn/boosters.html; break;}
    if ($flag = "006"){ rewrite "^/(.*)$" http://shop.web.cn/specials.html; break;}
    if ($flag = "007"){ rewrite "^(.*)/detail/ps_(.*)$" http://shop.web.cn/detail.html?goods_id=ps_$2; break;}
    if ($flag = "00"){ rewrite "^/(.*)$" http://shop.web.cn/; break;}
}
shop.web.cn
location / {
    set $flag -1;
    set $id -1;
    set $url $http_host$request_uri;
    if ($http_user_agent ~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") {
        set $flag 0;
        }
    if ($url ~* "(?=shop.web.cn)"){ set $flag "${flag}0"; }
    if ($url ~* "(?=main.html)"){ set $flag "${flag}1"; }
    if ($url ~* "(?=gold.html)"){ set $flag "${flag}2"; }
    if ($url ~* "(?=premium.html)"){ set $flag "${flag}3"; }
    if ($url ~* "(?=vehicles.html)"){ set $flag "${flag}4"; }
    if ($url ~* "(?=boosters.html)"){ set $flag "${flag}5"; }
    if ($url ~* "(?=specials.html)"){ set $flag "${flag}6"; }
    if ($url ~* "goods_id=(.*)"){ set $id $1; set $flag "${flag}7"; }
    if ($flag = "001"){ rewrite "^/(.*)$" http://mshop.web.cn/main.html; break;}
    if ($flag = "002"){ rewrite "^/(.*)$" http://mshop.web.cn/gold; break;}
    if ($flag = "003"){ rewrite "^/(.*)$" http://mshop.web.cn/premium; break;}
    if ($flag = "004"){ rewrite "^/(.*)$" http://mshop.web.cn/vehicles; break;}
    if ($flag = "005"){ rewrite "^/(.*)$" http://mshop.web.cn/boosters; break;}
    if ($flag = "006"){ rewrite "^/(.*)$" http://mshop.web.cn/specials; break;}
    if ($flag = "007"){ rewrite "^(.*)$" http://mshop.web.cn/detail/$id; break;}
    if ($flag = "00"){ rewrite "^/(.*)$" http://mshop.web.cn/; break;}
}

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«   2021年3月   »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
    网站收藏
    友情链接
    • Z-BlogPHP
    • 订阅本站的 RSS 2.0 新闻聚合

    Powered By Z-BlogPHP 1.6.5 Valyria

    Copyright Your WebSite.Some Rights Reserved.