iPhone detection and redirection

So you want detect iPhone users on your website and redirect them to a specific page or version of your site. Here is how to redirect iPhone traffic with .htaccess:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://m.example.com/ [R]

The code will redirect iPhone users to a iPhone specific site on m.example.com.

How iPhone detection and redirection works

The htaccess code works by examining the HTTP header “User-Agent” which all browser send. iPhone sends a User-Agent header similar to this one:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)
AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3

As you can see the text contains “iPhone”, and thats how you know its an iPhone user.

Detect and redirect ipad, Blackberry, etc.

In your main domain root folder, insert this .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteRule .* http://m.example.com/ [R]
RewriteCond %{HTTP_USER_AGENT} iPad
RewriteRule .* http://m.example.com/ [R]
RewriteCond %{HTTP_USER_AGENT} BlackBerry
RewriteRule .* http://m.example.com/ [R]
RewriteCond %{HTTP_USER_AGENT} Palm
RewriteRule .* http://m.example.com/ [R]

"Too many redirects" error

In your subdomain root folder, insert this .htaccess file:

RewriteEngine off

 

Post A Comment

Anti-Spam Quiz: