.htaccess Mobile Browser Redirect

Checkout the latest version of the ruleset first.
I just finished building out a simple little mobile site for hiphopdx.com. When I asked Google the best way to detect mobile browsers with PHP, it led to me this PHP code by Andy Moore. After a few minutes of contemplating the best way to include the code in the existing CMS framework I realized that it would probably be best to move the functionality outside of PHP. I mean, why clutter up display code with HTTP traffic manipulation. The website’s main traffic cop Apache is already perfectly capable of handling the duties already.

Here’s how to redirect mobile traffic using a .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
RewriteRule ^(.*)$ /m/ [L,R=302]

I did my best to translate the PHP code verbatim, if you want to understand what each line is doing you’ll want to check out the original code. Testing for all those user agents is probably a little overkill, but Mr Moore is a “dotMobi certified mobile web developer” I’m going to assume he knows what he’s talking about.

*The “tosh” user agent (I’m assuming this is for a Toshiba browser?) happens to catch browser on any browser on the Mac OS. I had to add !macintosh to skip macs. The iPhone/iPod Touch does not identify itself as “macintosh.”


Comments

66 responses to “.htaccess Mobile Browser Redirect”

  1. This code redirects laptops. During testing, a dell laptop with XP pro & IE7 got redirected

  2. This code redirects laptops. During testing, a dell laptop with XP pro & IE7 got redirected

  3. Could you please copy & paste your user agent string. I have tested this rewrite conditions extensively extensively – in fact I use a Dell laptop and have not had any problems with the IE7.

  4. Could you please copy & paste your user agent string. I have tested this rewrite conditions extensively extensively – in fact I use a Dell laptop and have not had any problems with the IE7.

  5. Ryan,

    This is great code…we’ve been putting it to work on our website for a while now, but we’re having trouble with it redirecting some browsers. I think it may be AOL browsers. The following User Agent is automatically redirected (though it shouldn’t be) when using your code:

    Mozilla/4.0 (compatible; MSIE 7.0; America Online Browser 1.1; rev1.5; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)

    Any thoughts? I’m trying to figure out which condition is matching it…any help would be appreciated.

  6. Ryan,

    This is great code…we’ve been putting it to work on our website for a while now, but we’re having trouble with it redirecting some browsers. I think it may be AOL browsers. The following User Agent is automatically redirected (though it shouldn’t be) when using your code:

    Mozilla/4.0 (compatible; MSIE 7.0; America Online Browser 1.1; rev1.5; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)

    Any thoughts? I’m trying to figure out which condition is matching it…any help would be appreciated.

  7. I think I’ve found the problem:

    RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|…

    the “eric” is matching the “Am*eric*a Online Browser line. Any ideas on how to match eric phones and exclude AOL browsers at the same time?

  8. I think I’ve found the problem:

    RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|…

    the “eric” is matching the “Am*eric*a Online Browser line. Any ideas on how to match eric phones and exclude AOL browsers at the same time?

  9. @Ben.
    It’s always nice to see that people are actually reading this blog.

    I think, tacking this on to the bottom of the list should work:
    RewriteCond %{HTTP_USER_AGENT} !america [NC]

  10. @Ben.
    It’s always nice to see that people are actually reading this blog.

    I think, tacking this on to the bottom of the list should work:
    RewriteCond %{HTTP_USER_AGENT} !america [NC]

  11. Wouldn’t it be simpler to just change eric to ericss ? Not sure what the ericsson browser is sending as a user agent.

  12. Wouldn’t it be simpler to just change eric to ericss ? Not sure what the ericsson browser is sending as a user agent.

  13. wouldnt you just use a service like handsetdetection.com so that you dont have to manage your own database of devices?

  14. wouldnt you just use a service like handsetdetection.com so that you dont have to manage your own database of devices?

  15. @david
    No.

    I’d rather not make a request to another service at the execution level. Making an HTTP request to another site before I decide which page to load has the potential to seriously impact the performance of my sites. I don’t like to hinge my user’s ability to load pages on external services. If the service or network connection to the service becomes slow, then my page load times are directly affected!
    Plus, I like to keep my PHP code really clean, I like to offload as much as possible. If I simple rewrite rule can replace all the overhead of contacting another site and parsing XML, then I’m going to opt for the simple light weight solution.

    Finally. I generally assume that a service from some start up I’ve never heard of, would not be capable of handling 500,000 – 1,000,000 request per day.

  16. @david
    No.

    I’d rather not make a request to another service at the execution level. Making an HTTP request to another site before I decide which page to load has the potential to seriously impact the performance of my sites. I don’t like to hinge my user’s ability to load pages on external services. If the service or network connection to the service becomes slow, then my page load times are directly affected!
    Plus, I like to keep my PHP code really clean, I like to offload as much as possible. If I simple rewrite rule can replace all the overhead of contacting another site and parsing XML, then I’m going to opt for the simple light weight solution.

    Finally. I generally assume that a service from some start up I’ve never heard of, would not be capable of handling 500,000 – 1,000,000 request per day.

  17. @David

    handsetdetection.com also seems like it’s in quite an infant stage, as it’s detecting my Internet Explorer 7 as Infogin Transcoder! More than half the visitors would be misdirected!

  18. @David

    handsetdetection.com also seems like it’s in quite an infant stage, as it’s detecting my Internet Explorer 7 as Infogin Transcoder! More than half the visitors would be misdirected!

  19. Sorry for amateur question, but I am confused.

    On below line do I replace /m/ with url of mobile url I want to send user?

    RewriteCond %{REQUEST_URI} !^/m/.*$

    I have normal site at domain.com and want mobile to go to sub.domain.com

    I have other different sites on domainx.com and want mobile users to go to same sub.domain.com, so, what do I put in /m/ on that line? I assume my sub.domain where mobi site is, correct?

    Also, RewriteRule ^(.*)$ /m/ [L,R=302] I add sub here too, correct?

  20. Sorry for amateur question, but I am confused.

    On below line do I replace /m/ with url of mobile url I want to send user?

    RewriteCond %{REQUEST_URI} !^/m/.*$

    I have normal site at domain.com and want mobile to go to sub.domain.com

    I have other different sites on domainx.com and want mobile users to go to same sub.domain.com, so, what do I put in /m/ on that line? I assume my sub.domain where mobi site is, correct?

    Also, RewriteRule ^(.*)$ /m/ [L,R=302] I add sub here too, correct?

  21. If you’re just wanting to redirect all mobi users to a subdomain, assuming the subdomain maps to a different directory on the server, you can:
    1) Depending on the way your directory structure is set up, you should be able to remove this line altogether.
    RewriteCond %{REQUEST_URI} !^/m/.*$
    What this line is doing causing rewrite engine to ignore the rules when browsing /m/. In my server configuration, without this rule I was getting an infinite loop. But since you’re using a subdirectory this is probably not going to be a problem.
    2) To actually redirect to the subdomain, change this:
    RewriteRule ^(.*)$ /m/ [L,R=302]
    to this:
    RewriteRule ^(.*)$ http://sub.domain.com/ [L,R=302]

    Hope that answers your question.

  22. If you’re just wanting to redirect all mobi users to a subdomain, assuming the subdomain maps to a different directory on the server, you can:
    1) Depending on the way your directory structure is set up, you should be able to remove this line altogether.
    RewriteCond %{REQUEST_URI} !^/m/.*$
    What this line is doing causing rewrite engine to ignore the rules when browsing /m/. In my server configuration, without this rule I was getting an infinite loop. But since you’re using a subdirectory this is probably not going to be a problem.
    2) To actually redirect to the subdomain, change this:
    RewriteRule ^(.*)$ /m/ [L,R=302]
    to this:
    RewriteRule ^(.*)$ http://sub.domain.com/ [L,R=302]

    Hope that answers your question.

  23. OMG – RYAN!!! You da bomb!!! Thanks for publishing the mobile redirect script I’ve been looking for, for over a year now! I changed the last line to point to my mobile sub diretory, pasted the script into my .htaccess page (above my error404 code), and I about fainted when it actually worked! I’m using a BlackBerry Curve 8330 and it worked perfectly.

    Thanks so much. I just got a Christmas present early this year!!!

    Frank B.
    http://mrtownhme.net

  24. OMG – RYAN!!! You da bomb!!! Thanks for publishing the mobile redirect script I’ve been looking for, for over a year now! I changed the last line to point to my mobile sub diretory, pasted the script into my .htaccess page (above my error404 code), and I about fainted when it actually worked! I’m using a BlackBerry Curve 8330 and it worked perfectly.

    Thanks so much. I just got a Christmas present early this year!!!

    Frank B.
    http://mrtownhme.net

  25. TYPO AL;ERT — that’s http://mrtownhome.net
    for those who are checking… 3 cups of coffee shy this morning.

  26. TYPO AL;ERT — that’s http://mrtownhome.net
    for those who are checking… 3 cups of coffee shy this morning.

  27. […] Mobile Redirection Using .htaccess ReWrite Rules Feb.18, 2009 in HowTo 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, […]

  28. Amazing script, much better than using php, asp, or junk

  29. Amazing script, much better than using php, asp, or junk

  30. Hi All,

    What if we change the whole methode…
    Not given every mobile browser IDENT but only the PC browser IDENTS… i think the lines will less and then you will have a good working redirect.

    If browser is: ie, mozilla, safari, bla bla then.. /
    otherwise redirect to /mobile/

    idea?

  31. Hi All,

    What if we change the whole methode…
    Not given every mobile browser IDENT but only the PC browser IDENTS… i think the lines will less and then you will have a good working redirect.

    If browser is: ie, mozilla, safari, bla bla then.. /
    otherwise redirect to /mobile/

    idea?

  32. @Peter
    There might be a way to get that to work. The problem is that opera, internet explorer and safari all have mobile versions.
    It’s worth investigating.

  33. @Peter
    There might be a way to get that to work. The problem is that opera, internet explorer and safari all have mobile versions.
    It’s worth investigating.

  34. Guys,
    I need some help here on the subdomian issue. I have copied and pasted the above code into my .htaccess file in the public_html folder. However, when I access the website via my Nokia N95 I get an “Unknown Response” error?

    See my code below:-
    RewriteEngine On
    RewriteCond %{HTTP_ACCEPT} “text/vnd.wap.wml|application/vnd.wap.xhtml+xml” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
    RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
    RewriteRule ^(.*)$ http://mobile.bowdenpublishing.co.uk/index.php/ [L,R=302]

  35. Guys,
    I need some help here on the subdomian issue. I have copied and pasted the above code into my .htaccess file in the public_html folder. However, when I access the website via my Nokia N95 I get an “Unknown Response” error?

    See my code below:-
    RewriteEngine On
    RewriteCond %{HTTP_ACCEPT} “text/vnd.wap.wml|application/vnd.wap.xhtml+xml” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
    RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
    RewriteRule ^(.*)$ http://mobile.bowdenpublishing.co.uk/index.php/ [L,R=302]

  36. Works and redirects properly, but images are not loading properly on mobile version. Maybe it’s a bug on my end. Any ideas?

  37. Works and redirects properly, but images are not loading properly on mobile version. Maybe it’s a bug on my end. Any ideas?

  38. Nevermind, it was my n00b-ish fault. My directory structure wasn’t correct. My subdomain was pointing to images from my regular domain. I guess images need to be underneath subdomain folders.

  39. Nevermind, it was my n00b-ish fault. My directory structure wasn’t correct. My subdomain was pointing to images from my regular domain. I guess images need to be underneath subdomain folders.

  40. Hey thank you so much for the script works great! One thing I wanted to ask is how can I force going back to original page if on my mobile page I have a link/choice for the visitor to go back to the original page, not the mobile version.

  41. Hey thank you so much for the script works great! One thing I wanted to ask is how can I force going back to original page if on my mobile page I have a link/choice for the visitor to go back to the original page, not the mobile version.

  42. @staz
    In short, set a cookie (something like “ignoreMobileRedirect”), get apache to check for the cookie.
    A full explanation probably requires a blog post.

  43. @staz
    In short, set a cookie (something like “ignoreMobileRedirect”), get apache to check for the cookie.
    A full explanation probably requires a blog post.

  44. Ok!
    All of these are nice and beautiful, but…
    I found a very intresting article on blogspot.com:

    “One of the most common problems for webmasters who run both mobile and desktop versions of a site is that the mobile version of the site appears for users on a desktop computer, or that the desktop version of the site appears when someone finds them from a mobile device. In dealing with this scenario, here are two viable options:

    Redirect mobile users to the correct version

    When a mobile user or crawler (like Googlebot-Mobile) accesses the desktop version of a URL, you can redirect them to the corresponding mobile version of the same page. Google notices the relationship between the two versions of the URL and displays the standard version for searches from desktops and the mobile version for mobile searches.

    If you redirect users, please make sure that the content on the corresponding mobile/desktop URL matches as closely as possible. For example, if you run a shopping site and there’s an access from a mobile phone to a desktop-version URL, make sure that the user is redirected to the mobile version of the page for the same product, and not to the homepage of the mobile version of the site. We occasionally find sites using this kind of redirect in an attempt to boost their search rankings, but this practice only results in a negative user experience, and so should be avoided at all costs.”

    http://googlewebmastercentral.blogspot.com/2009/11/running-desktop-and-mobile-versions-of.html

    How can I change redirection code if I want to allow the bot of Google-Mobile 2.1 inside my site without redirect? I don’t want to redirect the Google-Mobile Bot to another site.

    Google-Mobile 2.1 User Agent Details:
    Code:

    SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)

    Can someone help me how can I modify this redirection?

  45. Ok!
    All of these are nice and beautiful, but…
    I found a very intresting article on blogspot.com:

    “One of the most common problems for webmasters who run both mobile and desktop versions of a site is that the mobile version of the site appears for users on a desktop computer, or that the desktop version of the site appears when someone finds them from a mobile device. In dealing with this scenario, here are two viable options:

    Redirect mobile users to the correct version

    When a mobile user or crawler (like Googlebot-Mobile) accesses the desktop version of a URL, you can redirect them to the corresponding mobile version of the same page. Google notices the relationship between the two versions of the URL and displays the standard version for searches from desktops and the mobile version for mobile searches.

    If you redirect users, please make sure that the content on the corresponding mobile/desktop URL matches as closely as possible. For example, if you run a shopping site and there’s an access from a mobile phone to a desktop-version URL, make sure that the user is redirected to the mobile version of the page for the same product, and not to the homepage of the mobile version of the site. We occasionally find sites using this kind of redirect in an attempt to boost their search rankings, but this practice only results in a negative user experience, and so should be avoided at all costs.”

    http://googlewebmastercentral.blogspot.com/2009/11/running-desktop-and-mobile-versions-of.html

    How can I change redirection code if I want to allow the bot of Google-Mobile 2.1 inside my site without redirect? I don’t want to redirect the Google-Mobile Bot to another site.

    Google-Mobile 2.1 User Agent Details:
    Code:

    SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)

    Can someone help me how can I modify this redirection?

  46. Thanks Ryan. If it would be possible can you make a more detailed explanation on how to do this?

  47. Thanks Ryan. If it would be possible can you make a more detailed explanation on how to do this?

  48. fizzgig666 Avatar
    fizzgig666

    Thanks for the code! Works great for me. Would it be possible to add something that redirects to another link if no mobile device is detected rather than going to the default page?

Leave a Reply

Only people in my network can comment.