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. […] .htaccess Mobile Browser Redirect Oct.29, 2007 in HowTo UPDATE: New Ruleset Posted – February 2009 […]

  2. Xavier Avatar

    When i try to use the .htaccess file, my blackberry storm simply keeps requestting then says its has exceeded the amount of redirect attempts..not sure what i am doing wrong..

  3. You need to make sure that your replace “mobiledirectoryhere” with the directory you are using for your mobile site.

  4. You need to make sure that your replace “mobiledirectoryhere” with the directory you are using for your mobile site.

  5. You need to make sure that your replace “mobiledirectoryhere” with the directory you are using for your mobile site.

  6. What if we are doing a re-direct to a remote site? I am having an amazing problem getting this to work for some reason. I have broken the .htaccess file intentionally to verify that its reading it. But, I can’t seem to get it to detect a mobile browser for the life of me.

  7. What if we are doing a re-direct to a remote site? I am having an amazing problem getting this to work for some reason. I have broken the .htaccess file intentionally to verify that its reading it. But, I can’t seem to get it to detect a mobile browser for the life of me.

  8. What if we are doing a re-direct to a remote site? I am having an amazing problem getting this to work for some reason. I have broken the .htaccess file intentionally to verify that its reading it. But, I can’t seem to get it to detect a mobile browser for the life of me.

  9. …. as a followup… took out the request_uri line, moved it from .htaccess to the conf file, got nudged by someone that maybe turning off the rewrite engine might help, and voila…. works like a champ.

    much kudos in your direction sir!

  10. …. as a followup… took out the request_uri line, moved it from .htaccess to the conf file, got nudged by someone that maybe turning off the rewrite engine might help, and voila…. works like a champ.

    much kudos in your direction sir!

  11. …. as a followup… took out the request_uri line, moved it from .htaccess to the conf file, got nudged by someone that maybe turning off the rewrite engine might help, and voila…. works like a champ.

    much kudos in your direction sir!

  12. I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
    Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.

    Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

  13. I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
    Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.

    Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

  14. I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
    Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.

    Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

  15. Nice method. But Opera doesn’t behave. Try using Opera on a non-mobile device (i.e., desktop / laptop), and you’ll get redirected to the mobile site.

  16. Nice method. But Opera doesn’t behave. Try using Opera on a non-mobile device (i.e., desktop / laptop), and you’ll get redirected to the mobile site.

  17. Nice method. But Opera doesn’t behave. Try using Opera on a non-mobile device (i.e., desktop / laptop), and you’ll get redirected to the mobile site.

  18. @mike Doesn’t seem to be a problem on the production site I’m using similar rules on. Which version of opera?

  19. @mike Doesn’t seem to be a problem on the production site I’m using similar rules on. Which version of opera?

  20. @mike Doesn’t seem to be a problem on the production site I’m using similar rules on. Which version of opera?

  21. Hi,
    I think there is 2 way to resolve it:
    1) touch the httpd every time there is a new user agent that you rule don’t detect
    2) use a system that detect any type of device with a Device Repository

    I think that “Apache Mobile Filter” is a good method but obviously I am the owner of the project.

  22. Hi,
    I think there is 2 way to resolve it:
    1) touch the httpd every time there is a new user agent that you rule don’t detect
    2) use a system that detect any type of device with a Device Repository

    I think that “Apache Mobile Filter” is a good method but obviously I am the owner of the project.

  23. Hi,
    I think there is 2 way to resolve it:
    1) touch the httpd every time there is a new user agent that you rule don’t detect
    2) use a system that detect any type of device with a Device Repository

    I think that “Apache Mobile Filter” is a good method but obviously I am the owner of the project.

  24. @Idel
    That looks excellent.
    I’ll have to give it a good look. How stable is it?

  25. @Idel
    That looks excellent.
    I’ll have to give it a good look. How stable is it?

  26. @Idel
    That looks excellent.
    I’ll have to give it a good look. How stable is it?

  27. @RyanN

    The module, is stable.
    I did some performance test: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html?start=6

    This module is intalled on three univerisities mobile site.

  28. @RyanN

    The module, is stable.
    I did some performance test: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html?start=6

    This module is intalled on three univerisities mobile site.

  29. @RyanN

    The module, is stable.
    I did some performance test: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html?start=6

    This module is intalled on three univerisities mobile site.

  30. CraftyMomma Avatar
    CraftyMomma

    For the Opera problem change it from opera to OperaMini. I tested this and I can get regular opera browser to deliver the full site and used opera mini user agent string on Firefox and it delivered the mobile site.

  31. CraftyMomma Avatar
    CraftyMomma

    For the Opera problem change it from opera to OperaMini. I tested this and I can get regular opera browser to deliver the full site and used opera mini user agent string on Firefox and it delivered the mobile site.

  32. CraftyMomma Avatar
    CraftyMomma

    For the Opera problem change it from opera to OperaMini. I tested this and I can get regular opera browser to deliver the full site and used opera mini user agent string on Firefox and it delivered the mobile site.

  33. Works like a charm, thank you.. might just need to add a few more agents based on the country.

  34. Works like a charm, thank you.. might just need to add a few more agents based on the country.

  35. Works like a charm, thank you.. might just need to add a few more agents based on the country.

  36. When i try to use the .htaccess file, my blackberry storm simply keeps requestting then says its has exceeded the amount of redirect attempts..not sure what i am doing wrong..

  37. Thank you for this post, it will come in very handy. I know my way around regexp’s in php, but for whatever reason I always seem to have trouble using them in modrewrite. Idk if you can help, but I’d also like a way to detect the hardware a person is visiting on and display it. I have the motorola droid and when I get my mobile page up, I want to display to users what hardware they’re viewing the site on (even if it’s obvious because they’re using it.) I know it’s possible, but I don’t know how.

  38. ifuschini Avatar

    Just an update.
    The Apache Mobile Filter from November 2009 is included as module og Apache Module Registry, and it's used of several business site:
    http://www.idelfuschini.it/en/-success-historie

  39. Ryan –

    Many thanks from a New England-based expatCanuck.

    I'm an .html hack who's spent the past 24 hours looking for a quick & dirty way to redirect from my main site for mobile devices. I reviewed about a dozen sites, & yours was the first (and only) that allowed me to (expeditiously & successfully) redirect from just my main site to the mobile directory.

    Now I can put my energies where they belong — adapting the site for mobile.

    Many thanks.

    – Richard

  40. Glad to help!

  41. Ryan –
    One issue I found is that a link on my main mobile page to an external site (my sister's mySpace page) gets redirected to my main mobile page. Is there a way around this, either by creating another .htaccess exception line or by modifying the to direct opening in a new page?

  42. junglee_99 Avatar
    junglee_99

    what does /mobiledirectoryhere/ refer to?
    a complete url (eg http://www.yoursite.xxx/mobiledirectory/ )?

  43. ohryan Avatar

    That refers to the path you want to redirect mobile users to.

  44. […] using a .htaccess file which would be parsed by Apache. This mechanism is discussed in detail on this blog entry. For now however, let’s stick to our PHP […]

  45. That’s excatly what I was looking for! Good job old chap! Cheers!

  46. Ryan,

    I have another .htaccess redirect solution for you + a problem that needs to be solved:

    SOLUTION: To redirect mobile traffic to an external domain I use this one:

    RewriteEngine on
    RewriteCond %{HTTP_USER_AGENT} “Windows CE” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Ericsson” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Samsung” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “NetFront” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Palm OS” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Blazer” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Elaine” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “^WAP.*$” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Plucker” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “vodafone” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “iPhone” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “nokia” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “symbian” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “Opera Mini” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “BlackBerry” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “j2me” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “midp” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “htc” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “java” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “sony” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “android” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “AvantGo” [NC]
    RewriteRule (.*) http://www.SITENAME.mobi [R,L]

    PROBLEM: When at the mobi-domain (mobile site) and by clicking on a link that leads back to the full page desktop version of the site, … people don’t get there. They get redirected back to the mobi-domain. This is because of the .htaccess file in the root of the main desktop version domain.

    I’ve found something online that I think can solve the problem but I can’t get it to work. Here’s the quote and the .htaccess code:

    “The line below allows a user to choose to view the PC version by adding ?pc to the URL (e.g. from a specific switcher icon)”

    RewriteCond %{QUERY_STRING} !^pc$ [NC]

    How would I add the !^pc$ that the guy wrote about into my code so a Desktop version of the mobile site can be viewed without getting redirected back to the mobile site?

    In one of your older posts you say something about “In short, set a cookie (something like “ignoreMobileRedirect”), get apache to check for the cookie. A full explanation probably requires a blog post.” (Ref: http://ohryan.ca/blog/2007/10/29/htaccess-mobile-browser-redirect/#comment-27486894 )

    Could you please help me with a solution for this?
    Either with a cookie solution or with adding the !^pc$ to the .htaccess code I supplied?

    P.S: Could you please contact me on http://www.facebook.com/fredrikjacobsen.personal regarding this? I’ve been looking a long time for a solution…

    – Fredrik Jacobsen
    Norway

    1. Can you share the solution and code snippet to set the cookie and check it in .htaccess. I guess this same solution can be applied in httpd.conf.

    2. Can you share the solution and code snippet to set the cookie and check it in .htaccess. I guess this same solution can be applied in httpd.conf.

  47. Morningtime Avatar
    Morningtime

    The above original rules now treat the iPad as a mobile device, because the iPad user agent has “Mobile” in it. But it should render as a Desktop. How to prevent?

    I tried adding
    RewriteCond %{HTTP_USER_AGENT} !”iPad”[NC]

  48. Cheers spot on, saved me a lot of time writing the redirection!

    1.  Avatar
      Anonymous

      Thanks. These rules probably need some refinement, as a lot of the user agents it catches are most likely out of date.

  49. I have a client that would like macbook air to redirect to mobile version of the site. Is there a RewriteCond that would target the macbook air specifically??

    1.  Avatar

      No. The MacBook Air uses the same OS as every other mac, so there will not be any identifying info in the user-agent string. It’s sort of a weird request, imho. The 13″ MacBook Air has the same default screen resolution as a 13″ or 15″ MacBook Pro…

  50. […] The old redirection rules I posted attempt to redirect every mobile phone under the sun. At this point in 2011, it’s probably safe to completely ignore ancient phones and simplify your Apache rules in the process. apache mobile browser programming rewrite rule server in HowTo blog comments powered by Disqus /* Copyright © 2010 OhRyan.ca _uacct = "UA-2268568-1"; urchinTracker(); […]

Leave a Reply

Only people in my network can comment.