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.

67 thoughts on “Revisiting Mobile Redirection Using .htaccess ReWrite Rules

  1. 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..

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

  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. 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.

  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. …. 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!

  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. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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.

  20. 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..

  21. 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.

  22. 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

  23. 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

  24. 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]

  25. 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.

  26. 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.

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

  28. 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??

  29. 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…

  30. @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?

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

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

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

  34. 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

Your email address will not be published. Required fields are marked *