Redirect iPhone iPad Android with Apache mod_rewrite

iPhone detection and redirection using Apache rewrite rules is obvious. For example this code in .htaccess file will redirect iPhone users to a iPhone specific site.

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteRule .* http://www.seleniumguide.com/ [R]

In case there is a need to redirect iPhone, IPad and Android mobile devices, the code could be written as

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteCond %{HTTP_USER_AGENT} iPad [NC]
RewriteCond %{HTTP_USER_AGENT} Android [NC]
RewriteRule .* http://www.seleniumguide.com/ [R]

and could be simplified as

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iPhone|iPad|Android" [NC]
RewriteRule .* http://www.seleniumguide.com/ [R] 

The htaccess code works by examining a special string, called HTTP header "User-Agent" which the browser send. For exampe iPad sends a User-Agent header similar to this one

User-Agent: Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6531.21.10

What if we want to redirect users to mobile site and in the same time give them an ability to switch back to the full site version. Most likely it cannot be done using only Apache rewrite rules from mod_rewrite, but the problem could be solved by adding the cookies to the equation

//This code for redirecting user to the mobile site unless there is a cookie
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iPhone|iPad|Android" [NC]
RewriteCond %{HTTP_COOKIE} !^.*mobilesite=no.*$
RewriteRule ^.*$ https://m.domain.com [R=301]

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iPhone|iPad|Android" [NC]
RewriteCond %{HTTP_COOKIE} !^.*mobilesite.*$
RewriteRule ^.*$ https://m.domain.com [R=301,CO=mobilesite:yes:.domain.com:60]

This code handles the user who request non mobile site

RewriteEngine On
RewriteCond %{REQUEST_URI} ^.*fullversion.*$
RewriteRule ^.*$ https://domain.com [R,L,CO=mobilesite:no:.domain.com:60]

The cookie expiration time is set to 60 minutes and during this time user will access non mobile site version.

3 comments:

Michael said...

Very nice and concise tutorial!

Wagner Araujo said...

I used this code to creat a intro page in pad, so the visitor can shoose if he wannna go to the Appstore to check out our app or to go tho our website www.mundotri.com.br. The is when I click in the link(www.mundotri.com.br) it comes back to the intro page /apple/ and goes in a infinite loop.

Could you help me?

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iPhone|iPad" [NC]
RewriteCond %{HTTP_COOKIE} !^.*mobilesite=no.*$
RewriteRule ^.*$ http://mundotri.com.br/apple/ [R=301]

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iPhone|iPad|Android" [NC]
RewriteCond %{HTTP_COOKIE} !^.*mobilesite.*$
RewriteRule ^.*$ http://mundotri.com.br/apple/ [R=301,CO=mobilesite:yes:.mundotri.com.br:60]

RewriteEngine On
RewriteCond %{REQUEST_URI} ^.*fullversion.*$
RewriteRule ^.*$ http://mundotri.com.br[R,L,CO=mobilesite:no:.mundotri.com.br:60]

Anonymous said...

I just happened on this page looking for some sample code. Thanks to the author.

And to Wagner...

You are redirecting to the same host no matter if you are on a mobile device or not. This causes the infinite loop.

You have to send the mobile user to a different host or at least a different Apache VirtualHost (m.example.com) .