I have some special coding that only works for HTML5 browsers (simple geolocation stuff). I want to redirect iPhone and Android users to the m.example.com version of my website. How is the best way to do this?

3 Answers 3


you can use UserAgent detection, or you can use the jQuery.support(). Since you are looking for specific functionality, you can combine the two to get what you want.

UserAgent might actually be your best bet. Do this with a .htaccess file to cut down on your http requests, and possibly flashing your main site before redirection in the case of slow connection or slow javascript performance.

You can see the (mostly) exhaustive list of mobile user agents on the wikipedia page. As you can see there are similarities between manufacturers and versions that you could look for in a regular expression.

  • 3
    Doing this is the back-end is highly preferable over doing it with JS, especially as bandwidth on mobile devices is very narrow. the .htaccess method is the best bet, followed by a PHP/ASP/whatever backend method Jul 8, 2010 at 23:02

Your question touch two parts .. redirection and html5 compatibility.


I'm using the following .htaccess :

RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
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]
RewriteRule ^(.*)$ http://m.example.com/ [L,R=302]

For php you can use this library: http://detectmobilebrowsers.mobi/ ... but i strongly suggest using htaccess for processing time and security.

You can find some additional examples here


Browser capabilities

As my Android phone have no problem with html5 with geolocation code. You may consider using the Modernizr Javascript Library to detect the capabilities of the client browser visiting your page.

The excellent HTML5 boilerplate template is using modernizr out of the box to support this kind of needs.

My opinion is : I suggest to use the htaccess method for everything except recent phones and browsers supported by modernizr and let Android and iPhone users be checked by Modernizr for html5 feature support and redirect the "rest" on your "without geolocation" version.

Modernizr example :

 if (Modernizr.geolocation) {
    // do stuff
    // propose mobile version 

Hope this helps


If you're coding with PHP, you could use $_SERVER['HTTP_USER_AGENT'] for checking the UserAgent header, which is sent almost by any major mobile device to PHP script. Then just compare the value of this header with substr fuction to whatever it needs to have inside.

For example, Apple devices will always have 'iphone' or 'ipad' sub-strings; Android devices will always have 'android'. Almost every mobile device will have 'mobile' sub-string listed in this header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.