最近有一个需求,因为微信上默认是不能打开某宝链接的,但可以打开拼夕夕。于是做了一个自动跳转,通过对不同浏览器的user agent进行判断,实现不同浏览器跳转不同地址的功能。
下面直接上代码:
server {
listen 80;
server_name m.tbxe.com;
if ($http_user_agent ~* "(weixin|MicroMessenger)") {
return 301 https://mobile.yangkeduo.com/mall_page.html?mall_id=199384209;
}
if ($http_user_agent ~* "(mobile)") {
return 301 https://market.m.taobao.com/apps/market/shop/simpleshop.html?shopId=123897020;
}
return 301 https://shop123897020.taobao.com;
}
因为Nginx是不能使用 if else 语句的,所以只能变通一下了。使用301实现,而不是通过重写规则实现,可以减少请求。
虽然php、html、js等都可以实现这个功能,但本人觉得用nginx应该是效率最高的。