Javascript
1. Detect and redirect based on screen size
Below code will detect device’s screen size and if it’s less than 699 pixels, it will redirect it to your mobile enabled site (in this example m.yoursite.com).
Place the below code between <head> and </head> tags of your page. And replace “http://m.yoursite.com” to your own URL.
|
1 2 3 4 5 6 7 |
<script type="text/javascript">
<!--
if (screen.width <= 699) {
document.location = "http://m.yoursite.com";
}
//-->
</script> |
2. Detect and redirect if the device agent matches iPhone or iPod
Below code will specifically look out for iPhone and iPod devices, once matches, it will redirect them to your mobile enabled site (in this example m.yoursite.com).
Place the below code between <head> and </head> tags of your page. And replace “http://m.yoursite.com” to your own URL.
|
1 2 3 4 5 6 7 |
<script language=javascript>
<!--
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
location.replace("http://m.yoursite.com");
}
-->
</script> |
PHP
Open your header.php file, go to top of the document and hit <enter> to make an empty line before all the HTML codes start. Copy and paste the following code.
Don’t forget to replace “http://m.yoursite.com” to your own URL.
|
1 2 3 4 5 6 7 8 9 10 |
<?php function mobileDevice()
{
$type = $_SERVER['HTTP_USER_AGENT'];
if(strpos((string)$type, "Windows Phone") != false || strpos((string)$type, "iPhone") != false || strpos((string)$type, "Android") != false)
return true;
else
return false;
}
if(mobileDevice() == true)
header('Location: http://m.yoursite.com'); ?> |
Others
If you want to use other scripts, you may check out http://detectmobilebrowsers.com
