nginx 移动端和pc端自动跳转
场景
项 | 域名 | 描述 |
---|
pc端 | www.168seo.cn
| 用于pc端访问官网 |
移动端 | `m.168seo.cn | 用于移动端访问 |
现在的需求是这样,在pc端访问www.168seo.cn
和m.168seo.cn
都跳转到
www.168seo.cn
而在移动端访问www.168seo.cn
和m.168seo.cn
都跳转到m.168seo.cn
pc端conf
| erver
{
listen 443;
ssl on;
#listen [::]:80;
server_name www.168seo.cn 168seo.cn;
index index.html index.htm index.php default.html default.htm default.php;
# 真正用到的只有行
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) https://m.168seo.cn$1 permanent;
}
}
|
| server
{
...
listen 443;
ssl on;
#listen [::]:80;
server_name m.168seo.cn;
# 真正起作用的这行
if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
rewrite ^(.*) https://www.168seo.cn$1 permanent;
}
index index.html index.htm index.php default.html default.htm default.php;
...
}
|
需要注意的是 ,如果想让pc 跳转到移动 或者移动跳转到 pc 是302 临时重定向,可以修改 permanent 为 redirect
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
来自 https://www.168seo.cn/seo/24669.html