0

How to Detect if the visitor is logging by mobile and turn him to index.php and when logging from PC turn him to index.html

is that by .htaccess or what?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ahmed Nasser
  • 27
  • 1
  • 7
  • By `.htaccess`, you can follow this thread : http://stackoverflow.com/questions/3680463/mobile-redirect-using-htaccess – MTranchant Jan 09 '14 at 10:27
  • Are you asking specifically for a htaccess solution, or is the question open to alternative technologies? I ask due to the comment in my answer. – Ryan Jan 09 '14 at 10:42

3 Answers3

1

You need to use some sort of information provided by the client to determine if the user is on a mobile device or not.

There any many ways to do this, for example in JavaScript

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    /* User is mobile... */
}

Which will check for a string used in a mobile device in the user agent.

There are libraries such as Modernizr (http://www.modernizr.com/) which have things like this to help you.

All in all, there's no set way to do it - you'll have to try different methods and choose the one you like.

Ryan
  • 3,552
  • 1
  • 22
  • 39
  • 1
    Where in tags do you see JavaScript? – Yang Jan 09 '14 at 10:29
  • 1
    @djay the original post states 'is that by .htaccess or what?' which indicates that OP isn't sure on the technologies used to do this. For that reason, I gave the ***example*** of using JavaScript. – Ryan Jan 09 '14 at 10:31
0

You can use these rules in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# forward mobile users to index.php
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]
RewriteRule ^ - [E=ISMOBILE:1]

RewriteCond %{ENV:ISMOBILE} =1
RewriteRule !^index\.php$ index.php [L]

# otherwise desktop users to index.html
RewriteCond %{ENV:ISMOBILE} !=1
RewriteRule !^index\.html$ index.html [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

It can be done with this php script, it will return a true if mobile, some mobile user agents are missing

<?php
    // ------- DETECT USER DEVICE ----------
    $user_device = "";
    $IsMobile = "";
    $agent = $_SERVER['HTTP_USER_AGENT'];
    if (preg_match("/Valve/", $agent)) {
       $user_device = "Steam GameOverlay";
    } else if (preg_match("/Safari/", $agent)) {
        $user_device = "Safari";
    } else if (preg_match("/Android/", $agent)) {
        $user_device = "Android Mobile";
    } else if (preg_match("/IEMobile/", $agent)) {
        $user_device = "Windows Mobile";
    } else if (preg_match("/Chrome/", $agent)) {
        $user_device = "Google Chrome";
    } else if (preg_match("/MSIE/", $agent)) {
        $user_device = "Internet Explorer";
    } else if (preg_match("/Firefox/", $agent)) {
        $user_device = "Firefox";
    } else if (preg_match("/Opera/", $agent)) {
        $user_device = "Opera";
    }
    $OSList = array
    (
            // Match user agent string with operating systems
            'Android' => 'Android',
            'Windows 3.11' => 'Win16',
            'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
            'Windows 98' => '(Windows 98)|(Win98)',
            'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
            'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
            'Windows Server 2003' => '(Windows NT 5.2)',
            'Windows Vista' => '(Windows NT 6.0)',
            'Windows Phone' => '(XBLWP7)|(ZuneWP7)|(Windows Phone OS 7.5)|(Windows Phone OS 7.0)|(Windows Phone 8.0)',
            'Windows 8' => '(Windows NT 6.2)',
            'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)',
            'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
            'Windows ME' => 'Windows ME',
            'Open BSD' => 'OpenBSD',
            'Sun OS' => 'SunOS',
            'Linux' => '(Linux)|(X11)',
            'iPhone' => 'iPhone',
            'iPad' => 'iPad',
            'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
            'QNX' => 'QNX',
            'BeOS' => 'BeOS',
            'OS/2' => 'OS/2',
            'Mac OS' => 'Mac OS',
            'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
    );

    // Loop through the array of user agents and matching operating systems
    foreach($OSList as $CurrOS=>$Match) {
            // Find a match
            if (@eregi($Match, $agent)) {
                    break;
            } else {
                $CurrOS = "Ukendt OS";
            }
    }
    if ($user_device == ""){
    $user_device = "Ukendt Browser";
    }
    //$device = "$user_device : $CurrOS";
    $device = "$CurrOS";
    // ------- END DETECT USER DEVICE ----------

    if ($CurrOS == "Android" || $CurrOS == "Windows Phone" || $CurrOS == "iPhone"){
        $IsMobile = "True ".$CurrOS; 
    }else{
        $IsMobile = "False ".$CurrOS;
    }
?>
AlexanderYW
  • 217
  • 1
  • 7
  • I wouldn't recommend using `eregi` as you have done. `This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.` http://uk1.php.net/eregi – Ryan Jan 09 '14 at 10:43