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.

  • Pingback: .htaccess Mobile Browser Redirect - OHRYAN.CA()

  • Xavier

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

  • http://ohryan.ca/ RyanN

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

  • http://ohryan.ca/ RyanN

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

  • http://ohryan.ca/ RyanN

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

  • James

    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.

  • James

    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.

  • James

    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.

  • James

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

  • James

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

  • James

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

  • http://www.idelfuschini.it/ Idel

    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

  • http://www.idelfuschini.it/ Idel

    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

  • http://www.idelfuschini.it Idel

    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

  • http://mineral.pro/ Mike

    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.

  • http://mineral.pro/ Mike

    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.

  • http://mineral.pro Mike

    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.

  • http://ohryan.ca/ RyanN

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

  • http://ohryan.ca/ RyanN

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

  • http://ohryan.ca/ RyanN

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

  • http://www.idelfuschini.it/ Idel

    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.

  • http://www.idelfuschini.it/ Idel

    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.

  • http://www.idelfuschini.it Idel

    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.

  • http://ohryan.ca/ RyanN

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

  • http://ohryan.ca/ RyanN

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

  • http://ohryan.ca/ RyanN

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

  • http://www.idelfuschini.it/ Idel

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

  • http://www.idelfuschini.it/ Idel

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

  • http://www.idelfuschini.it Idel

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

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

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

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

  • stephan

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

  • stephan

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

  • stephan

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

  • Xavier

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

  • http://infectionist.com HaLo2FrEeEk

    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.

  • ifuschini

    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

  • http://oldWithoutMoney.com Richard

    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

  • ohryan

    Glad to help!

  • http://oldWithoutMoney.com Richard

    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?

  • junglee_99

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

  • ohryan

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

  • Pingback: Mobile Device Detection and Redirection using PHP | mlynn()

  • http://www.mobilewebfx.com Mobile Web Designer

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

  • http://www.iphonemobilwebside.no Fredrik Jacobsen

    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

  • 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]

  • Guest

    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.

  • Guest

    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.

  • Admin

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

  • Anonymous

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

  • Anne

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

  • Anonymous

    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…

  • Pingback: Modern Mobile Redirect Using .htaccess | OhRyan.ca()

  • Pingback: AlanTello.com » Blog Archive » Redireccionar móviles con .htaccess()

  • http://ptext.mobi Presse Mobil

    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.

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

  • 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?

  • http://twitter.com/digisolved Darren Ramsden

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

  • Anonymous

    Not off hand, sorry.

  • Alseaf3

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

  • Anonymous

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

  • Brian

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

  • Anonymous

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

  • http://www.gofleet.ca/ gps tracking

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

  • http://www.messyfloors.com/ Queen Creek carpet cleaning

    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.