pc端、移动端的识别与跳转
发布时间 2024-06-12 23:01:33

  本文摘自:自动识别 pc 端、移动端,并跳转,有兴趣可以去看一下原文作者。本文仅做记录,方便自己查阅。

js 识别

1
2
3
4
5
6
7
8
9
if (
/Android|webOS| iPhone | iPad | iPod |BlackBerry|opera mini|opera mobile|appleWebkit.*mobile|mobile/i.test(
navigator.userAgent
)
) {
window.location.replace('./mobile-html/index.html')
} else {
window.location.replace('./pc-html/index.html')
}

nginx 识别

1
2
3
4
5
6
location / {
if ($http_user_agent ~* "(Android|webOS|iPhone|iPad|iPod|BlackBerry|Opera Mini|Opera Mobile|AppleWebKit.*Mobile|Mobile)") {
rewrite ^(.*)$ /mobile-html/$1 break;
}
rewrite ^(.*)$ /pc-html/$1 break;
}

  或者

1
2
3
4
5
6
7
8
location / {
root /usr/local/website/web;
if ($http_user_agent ~* "(Android|webOS|iPhone|iPad|iPod|BlackBerry|Opera Mini|Opera Mobile|AppleWebKit.*Mobile|Mobile)") {
root /usr/local/website/mobile;
}

index index.html index.htm;
}
上一页
2024-06-13 10:11:28
下一页