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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17276077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:36:49  来源:igfitidea点击:

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

phpjavascriptjquerycsshtml

提问by Amarjeet Singh

I have a website www.domain.com and I have designed a new mobile version m.domain.com. But how to detect the mobile device and redirect them to m.domain.com from www.domain.com?

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

回答by vikingmaster

I just searched in google and here is the first result:

我刚刚在谷歌搜索,这是第一个结果:

http://mobiledetect.net/

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())

回答by Vivek Sadh

Use this code for detecting mobile devices and then redirect it:-

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

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

The test()method has been used to test for a match in a string and if a match is found then a redirection takes place. Add this code at the top of the page.

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