0

I have this code on desktop website www.domain.com:

<?php
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
if(!preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $userAgent)) 
{
if(strpos($_SERVER["HTTP_HOST"], "m.domain.com") !== false){        
    echo "<script>window.location='";
    echo str_replace("//m." , "//www.", "https://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
    echo "';</script>";
}
}
?>

And this code on mobile website m.domain.com:

<?php
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
if(preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])) 
{
if(strpos($_SERVER["HTTP_HOST"], "www.domain.com") !== false){      
    echo "<script>window.location='";
    echo str_replace("//www." , "//m.", "https://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
    echo "';</script>";
}
}
?>

Unable to detect any issue in this code but sites are redirecting infinitely from one to another. Using on Wordpress website, if Wordpress limits use of this php code, please guide.

XIMRX
  • 2,130
  • 3
  • 29
  • 60

4 Answers4

1

I have found the following solution to achieve your problem, i hope this will helps you.

add_action('wp_head', 'redirect_mobile');
function redirect_mobile(){
   if ( wp_is_mobile() ) {
    wp_redirect( 'http://url' ); 
    exit; 
  }
}

You can add the above line of code in your themes functions.php file. I will found the given link to helps you more on this topic

Niket Joshi
  • 739
  • 5
  • 23
0

you can try with below solution

 <?php 
    $iphone  = strpos($_SERVER['HTTP_USER_AGENT'],'iPhone');
    $android = strpos($_SERVER['HTTP_USER_AGENT'],'Android');
    $berry   = strpos($_SERVER['HTTP_USER_AGENT'],'BlackBerry');
    $ipod    = strpos($_SERVER['HTTP_USER_AGENT'],'iPod');

    if ($iphone || $android || $palmpre || $ipod || $berry == true)
    {
       echo "<script>// <![CDATA[
                window.location='http://m.site.com'
            // ]]></scrip>";
    }
 ?>

Reference URL : https://www.woorank.com/en/blog/how-to-redirect-mobile-users-on-your-website

  • The code you posted is broken, its correct form is here as stated by your reference: https://wallydavid.com/simple-php-mobile-website-redirect-code/ It is similar code causing the same problem, only handling fewer useagents – XIMRX May 20 '19 at 12:30
  • changed it please check now. – Nikunj Bambhroliya May 20 '19 at 12:31
0

There's a PHP library available for this, which gives you a lot of options like mobile, tablet, iOS, Android, etc.: http://mobiledetect.net/

Since you're in PHP, you can redirect by setting the header instead of using JavaScript, which is also faster: How do I make a redirect in PHP?

Sjors
  • 1,205
  • 1
  • 8
  • 24
0

I know is not a straight answer to your question, but consider using existing libraries like Mobile-Detect for example. Is quite straightforward and you will support the redirect for a lot more mobile devices.

$detect = new Mobile_Detect;
$isMobile = $detect->isMobile();
if ($isMobile) {
    // do the redirect here
}
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85