Redirect iPhone and Android Phones to a Mobile Website

A very easy way to redirect users that visit your website from a mobile device to the mobile version of your website, is using your htaccess file. If your server is running on Apache, you can add the following code to your .htaccess file to get the job done:

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|android" [NC]
RewriteCond %{HTTP_HOST} !^m.example.com
RewriteRule ^(.*)$ http://m.example.com/ [L,R=302]
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|android" [NC]
RewriteCond %{HTTP_HOST} !^m.example.com
RewriteRule ^(.*)$ http://m.example.com/ [L,R=302]

 

You can include other mobile browsers by adding other browser to the RewriteCond rule:

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