-2

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?

Amarjeet Singh
  • 21
  • 1
  • 1
  • 2

2 Answers2

3

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

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