Revisiting Mobile Redirection Using .htaccess ReWrite Rules

This article is out of date, check my latest code snippet here.
Back in 2007 I wrote a post detailing a method for detecting and redirecting mobile browsers using .htaccess. Since then the mobile landscape has changed quite a bit: iPhone and Android have been released, the PDA market is all but dead and phones are better in general.

My original post was based on a PHP script by Andy Moore. It’s worth noting that he has continued to update his script and now has a fancy little generator to help you create a PHP based redirect ruleset. Check out detectmobilebrowsers.mobi for more info.

His solution will probably work for a lot of applications, but there are 2 main reasons I prefer using Apache’s rewrite rules to redirect mobile browsers:
1) Application independent. By putting the rules in the root .htaccess file you can easily redirect traffic across multiple directories, independent of any webapp that might or might not be running in those directories.
2) Faster. Placing the rewrite rules in apache’s httpd.conf file is potentially faster, than running the regexp in a php script for every request.

Enough of that, here’s the updated ruleset.

# don't apply the rules if you're already in the mobile directory, infintine loop
# you'll want to test against the host if you're using a subdomain
RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
# if the browser accepts these mime-types, it's definitely mobile, or pretending to be
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
# a bunch of user agent tests
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]
# put your rewrite rules here

Text Version

As you can see, the user agent list is a fair bit less verbose. I’ve been testing the ruleset on a very busy site, based on over 250k mobile site hits this set of rules is causing a lot fewer false positives, while not missing any significant number of mobile browsers.


Comments

67 responses to “Revisiting Mobile Redirection Using .htaccess ReWrite Rules”

  1. I would suggest replacing opera with opera mini in the Rewrite conditions, otherwise all the desktop users will be detected as mobile users.

    see:
    http://www.useragentstring.com/pages/Opera/
    http://www.useragentstring.com/pages/Opera%20Mini/

    Opera Mobile is already caught by the mobile string.

    1.  Avatar
      Anonymous

      Good catch, I’ve updated this code since this post (http://ohryan.ca/blog/2011/01/21/modern-mobile-redirect-using-htaccess/) but failed to include Opera.

      Thanks for the suggestion.

  2. Borinteen Avatar
    Borinteen

    @ohryan:disqus Do you have any code examples on once you’ve created a htaccess file that redirects a mobile devices from the full desktop site to redirect to the mobile site. Then have the option on the mobile site to go to the full desktop site. I think i’ve read you can do this with a cookie?

    1.  Avatar
      Anonymous

      Not off hand, sorry.

  3. This method works great (after a little tweaking) – I use it for http://www.digisolved.com and it works great!

  4. Alseaf3 Avatar

    I want to redirect all non-mobile users to a message saying ‘please visit us on a mobile device’. How do i do that?

    1.  Avatar
      Anonymous

      Interesting.  Basically, put a “!” (aka “not”) infront of the rules and make the destination URL the page with your message.

  5. if I do not want the ipad to be part of it, what do i delete?

    1.  Avatar
      Anonymous

      Take a look at my newer post: http://ohryan.ca/blog/2011/01/21/modern-mobile-redirect-using-htaccess/ and delete “ipad”. Voila!

  6. Great to know the — in depth from this blog.This will really help for my forward steps to be taken.

  7. Everyone
    knows how carpets entice filth so much. Even if we try to take care and keep
    our carpets clear, there’ll all the time be a stain that will start to come out
    of it.

  8. Hey, i have implemented the same redirects and it worked, but a new situation arises and i need a specific folder to be restricted from redirects, and other pages too. Can you help me with the code. Thanks in advance

Leave a Reply

Only people in my network can comment.