如何将移动用户重定向到我网站的移动版本


How to redirect mobile users to mobile version of my site?

我有一个网站www.domain.com,我设计了一个新的移动版本m.domain.com。但是如何检测移动设备并将其从www.domain.com重定向到m.domain.com?

我刚刚在谷歌上搜索了一下,结果是:

http://mobiledetect.net/

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();
// Check for any mobile device.
if ($detect->isMobile())
// Check for any tablet.
if($detect->isTablet())
// 3. Check for any mobile device, excluding tablets.
if ($detect->isMobile() && !$detect->isTablet())

使用此代码检测移动设备,然后重定向:-

<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "m.domain.com";
}
</script>

test()方法已用于测试字符串中的匹配,如果找到匹配,则会进行重定向。在页面顶部添加此代码。