Page 1 of 1

Redirect Mobile Browsers

Posted: Sat Feb 13, 2010 6:34 pm
by Nullig
With the increasing use of handheld devices to surf the net, more and more of the sites I do want to offer completely different content/styles to mobile users.

One way to accomplish this is to have a separate subdomain for mobiles and redirect them via the .htaccess file:

Code: Select all

RewriteEngine on
RewriteBase /

# prevent looping
RewriteCond %{HTTP_HOST} !^mobile.yoursite.com$

# if the browser accepts these mime-types, it's definitely mobile, or pretending to be
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]

# a bunch of user agent tests
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]

# put your rewrite rules here
RewriteRule ^(.*)$ http://mobile.yoursite.com/  [L,R=302]
Note: This example assumes a subdomain of "mobile".

To see what your site looks like on a mobile phone, you can use these emulators:

http://emulator.mtld.mobi/emulator.php
http://www.testiphone.com/

Nullig

Re: Redirect Mobile Browsers

Posted: Fri Feb 18, 2011 11:21 am
by faglork
Nullig wrote:With the increasing use of handheld devices to surf the net, more and more of the sites I do want to offer completely different content/styles to mobile users.

One way to accomplish this is to have a separate subdomain for mobiles and redirect them via the .htaccess file:
But in that case you need to maintain TWO completely different sites, which adds a lot of work, especially with large sites. Wouldn't it be simpler to incorporate the browser sniffing into the template and just call a different CSS? Or would it even be possible to switch templates?

E.g. for WORDPRESS there exists a plugin which just sends a completely different theme to mobile devices, and it works great.

Cheers,
Alex

Re: Redirect Mobile Browsers

Posted: Fri Feb 18, 2011 6:37 pm
by Nullig
Yes, it does require maintaining 2 sites, but in some cases you want to show different content - more concise content.

The maintenance is minimal, as the template and stylesheets are bare bones.

Nullig

Re: Redirect Mobile Browsers

Posted: Thu Mar 31, 2011 8:59 pm
by jtcreate
Great thread. Great script. I got this working today, but have one minor issue.

If I'm an iPhone user or other with full browser capability, I'd like to show the mobile version first, but have the option for the user to click "view the full site" and have it show.

It's setup like this:
"http://www.domain.com" for main site
"http://www.domain.com/mobile" for mobile site

Redirect sends mobile user to /mobile directory and it works great. But, clicking to view full site just loops them back to the mobile directory.

Any tricks to making this work? Thanks!

Re: Redirect Mobile Browsers

Posted: Fri Apr 01, 2011 7:28 am
by fredp
jtcreate wrote: ...
Redirect sends mobile user to /mobile directory and it works great. But, clicking to view full site just loops them back to the mobile directory.

Any tricks to making this work? Thanks!
Hi,

How about setting a cookie when a user opts for the full site and then check for the cookie in the .htaccess file, with another RewriteCond? E.g.:
...
# prevent looping
RewriteCond %{HTTP_COOKIE} !_full_site_please_blah_blah_blah
...

Re: Redirect Mobile Browsers

Posted: Wed Apr 06, 2011 6:15 am
by lemonaid
Hi there

I came up with a new solution for this. I also wanted to send mobile users off to separate 'mobile template' pages and iphone users to the main site.

BUT I wanted my visitors to be able to switch between the two.

The solution that works for me is to place a php script into a default.php file as so:

This is an adaptation from:

Mobile version detection
-----------------------------------------------------
compliments of http://www.buchfelder.biz/


Code: Select all

 
<?php

if($mobile){
   header('Location: http://www.mydomain.com.au/mobile.htm');
   } else {
   header('Location: http://www.mydomain.com.au/index.php');
   }

$text = $_SERVER['HTTP_USER_AGENT'];
$var[0] = 'Mozilla/4.';
$var[1] = 'Mozilla/3.0';
$var[2] = 'AvantGo';
$var[3] = 'ProxiNet';
$var[4] = 'Danger hiptop 1.0';
$var[5] = 'DoCoMo/';
$var[6] = 'Google CHTML Proxy/';
$var[7] = 'UP.Browser/';
$var[8] = 'SEMC-Browser/';
$var[9] = 'J-PHONE/';
$var[10] = 'PDXGW/';
$var[11] = 'ASTEL/';
$var[12] = 'Mozilla/1.22';
$var[13] = 'Handspring';
$var[14] = 'Windows CE';
$var[15] = 'PPC';
$var[16] = 'Mozilla/2.0';
$var[17] = 'Blazer/';
$var[18] = 'Palm';
$var[19] = 'WebPro/';
$var[20] = 'EPOC32-WTL/';
$var[21] = 'Tungsten';
$var[22] = 'Netfront/';
$var[23] = 'Mobile Content Viewer/';
$var[24] = 'PDA';
$var[25] = 'MMP/2.0';
$var[26] = 'Embedix/';
$var[27] = 'Qtopia/';
$var[28] = 'Xiino/';
$var[29] = 'BlackBerry';
$var[30] = 'Gecko/20031007';
$var[31] = 'MOT-';
$var[32] = 'UP.Link/';
$var[33] = 'Smartphone';
$var[34] = 'portalmmm/';
$var[35] = 'Nokia';
$var[36] = 'Symbian';
$var[37] = 'AppleWebKit/413';
$var[38] = 'UPG1 UP/';
$var[39] = 'RegKing';
$var[40] = 'STNC-WTL/';
$var[41] = 'J2ME';
$var[42] = 'Opera Mini/';
$var[43] = 'SEC-';
$var[44] = 'ReqwirelessWeb/';
$var[45] = 'AU-MIC/';
$var[46] = 'Sharp';
$var[47] = 'SIE-';
$var[48] = 'SonyEricsson';
$var[49] = 'Elaine/';
$var[50] = 'SAMSUNG-';
$var[51] = 'Panasonic';
$var[52] = 'Siemens';
$var[53] = 'Sony';
$var[54] = 'Verizon';
$var[55] = 'Cingular';
$var[56] = 'Sprint';
$var[57] = 'AT&T;';
$var[58] = 'Nextel';
$var[59] = 'Pocket PC';
$var[60] = 'T-Mobile';    
$var[61] = 'Orange';
$var[62] = 'Casio';
$var[63] = 'HTC';
$var[64] = 'Motorola';
$var[65] = 'Samsung';
$var[66] = 'NEC';

$result = count($var);

for ($i=0;$i<$result;$i++)
{    
    $ausg = stristr($text, $var[$i]);    
    if(strlen($ausg)>0)
    {
        header("location: $mobile");
        exit;
    }
    
}
?>
And then just added this to the .htaccess

Code: Select all

DirectoryIndex default.php index.php index.htm index.html

My question is though "Will this have a negative effect on the Search Engines?"

Re: Redirect Mobile Browsers

Posted: Tue Jan 24, 2012 10:09 pm
by fearmydesign
Hi guys, so here's what I did to redirect mobile users to a different CMSMS template, please let me know if this is a good approach, so far it seems to be working.

I just created a Global Content Block with the following code to redirect users;

Code: Select all

{literal}
<__script__ type="text/javascript">
if ( (screen.width < 1024) && (screen.height < 768) ) { 
window.location = 'http://www.website.com/mobile.htm';
}
</__script>
{/literal}
...and placed this at the top of my home page template:

Code: Select all

{global_content name='mobilescript'}
Not sure if its the best approach, but seems to be working, please advice. Thx

Re: Redirect Mobile Browsers

Posted: Tue Jan 24, 2012 10:19 pm
by calguy1000
A simple UDT would do it... something like:

Code: Select all

$browser = cge_utils::get_browser();
if( $browser->isMobile() )
{
   redirect('http://m.website.com');
}

Re: Redirect Mobile Browsers

Posted: Thu Jan 26, 2012 6:37 pm
by Dr.CSS
Why not just use CSS to do this?...

http://multiintech.com/testing.html

it doesn't look so good on the first emulator using the Nokia N70 but the other works ok, of course I'm not done with it...