Redirect Mobile Browsers

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Redirect Mobile Browsers

Post 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
Last edited by Nullig on Sun Feb 14, 2010 1:42 am, edited 1 time in total.
faglork

Re: Redirect Mobile Browsers

Post 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
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Redirect Mobile Browsers

Post 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
User avatar
jtcreate
Forum Members
Forum Members
Posts: 168
Joined: Wed Mar 21, 2007 11:01 am
Location: Grand Rapids, MI

Re: Redirect Mobile Browsers

Post 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!
Last edited by Dr.CSS on Thu Jan 26, 2012 6:33 pm, edited 1 time in total.
Reason: Please use double quotes on fake links so they aren't clickable...
fredp
Forum Members
Forum Members
Posts: 218
Joined: Sun Jul 27, 2008 1:36 am
Location: USA

Re: Redirect Mobile Browsers

Post 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
...
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
User avatar
lemonaid
Forum Members
Forum Members
Posts: 77
Joined: Mon Sep 22, 2008 7:44 am

Re: Redirect Mobile Browsers

Post 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?"
User avatar
fearmydesign
Power Poster
Power Poster
Posts: 363
Joined: Sun Feb 28, 2010 10:54 pm

Re: Redirect Mobile Browsers

Post 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
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Redirect Mobile Browsers

Post 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');
}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12709
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: Redirect Mobile Browsers

Post 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...
Post Reply

Return to “Tips and Tricks”