Mobile Site Redirection

Discussion in 'General' started by narsaw, Dec 15, 2011.

  1. narsaw

    narsaw New Member

    Hello my site (http://www.domain.com) I have recently create a MOBILE version of this site (http://m.domain.com).

    Here is what I want to do. When a user arrives at my main site (http://www.domain.com) vis a mobile or smartphone (iPhone, Droid,etc) I want to automatically redirect them to the mobile version of my site. If the user arrives directly to my to my mobile site, no redirection is done.

    Also a cookie is set to remember their user desires (mobile=1 or mobile=0)

    I have done considerable research online and read many post over at Stackoverflow, for example this one.

    http://stackoverflow.com/questions/7505939/htaccess-mobile-cookie-set
    and
    http://stackoverflow.com/questions/3680463/mobile-redirect-using-htaccess

    Generally it is recommended to do this type of redirection via htaccess rather than in your php site template files. So I would like to try this method.

    Here is the code I added to my .htaccess but cannot get it work it seems NOT to be setting the cookie.

    Code:
    
    # Check if mobile=1 is set and set cookie 'mobile' equal to 1
    RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
    RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
    
    # Check if mobile=0 is set and set cookie 'mobile' equal to 0
    RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
    RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
    
    # cookie can't be set and read in the same request so check
    RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
    RewriteRule ^ - [S=1]
    
    # Check if this looks like a mobile device
    RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
    RewriteCond %{HTTP:Profile} !^$
    
    # Check if we're not already on the mobile site
    RewriteCond %{HTTP_HOST} !^m\.
    # Check to make sure we haven't set the cookie before
    RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
    # Now redirect to the mobile site
    RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]
    

    Can you assist me in sorting this out? Using Litespeed server on Hawkhost webhost. They recommend that I ask here.
  2. NiteWave

    NiteWave Administrator

    how about this simple one:

    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
    RewriteRule (.*) http://m.domain.com/$1 [R,L]
  3. narsaw

    narsaw New Member

    Hello NiteWave,

    Thanks for the fast response. I like the idea of simplicity but there is an issue with your suggestion.

    Best practices for mobile site design is to always give the user an option of viewing the "Full HTML" version of your site.

    In code you provided, the user will always be stuck perpetually on the mobile version because of the user-agent identification will not change.

    This is why is beneficial to check against a cookie, if "mobile=0", then dont apply a redirect rule.

    Would love to hear some more suggestions.
  4. NiteWave

    NiteWave Administrator

    if you use cookie, for
    RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)

    should use HTTP_COOKIE instead of QUERY_STRING

Share This Page