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?
Asked
Active
Viewed 2.2k times
-2
-
2Link to the duplicate would be great. Are we talking about the post from 2009? Or 2010? – Commandrea Dec 18 '13 at 18:13
2 Answers
3
I just searched in google and here is the first result:
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())

sybear
- 7,837
- 1
- 22
- 38
1
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.

Vivek Sadh
- 4,230
- 3
- 32
- 49
-
@ vivek and how to redirect to mobile version... it will be beetter if u can give me the whole code – Amarjeet Singh Jun 24 '13 at 13:07
-
-
@ vivek Thanks for your help.But can you tell me with where to include in this code?and if any thing else is needed to include with this code. – Amarjeet Singh Jun 24 '13 at 14:23
-
-
You should not redirect with JavaScript because you cannot set the proper response headers (301, 302) and that is bad for SEO. Use PHP as noted by Jari below. – Justin Nov 22 '13 at 17:53