.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. I don't see how you would do that without directing all traffic to that page by default. Or is that what you are wondering how to do?

  2. fizzgig666 Avatar
    fizzgig666

    I'm working on a test site and do not have access to the clients server. Just wanted to make my test site redirect to their site in a regular browser for show. I guess I could just use a redirect script on the default page to get the same effect.

  3. […] my original posts on mobile redirection in .htaccess files I’ve gotten some comments and emails asking […]

  4. Thank you very much!

  5. […] Bisschen googlen hilft meist schon .htaccess Mobile Browser Redirect | Canadian Tech News, Hacks & How Tos from OhRyan.ca greetz __________________ […]

  6. It’s a great code, I only have one bug. That needs attention. On a Vista machine it recognises IE8 as a mobile browser and redirects!

  7. […] .htaccess Mobile Browser Redirect […]

  8. thanks for the post….

  9. Hi:

    This is exactly what I was looking for, but what can I do if I don’t want the iPad browser to be redirected?

    I’m kinda of noob in all this, so any help is really appreciated!!

    Rgds

    1.  Avatar
      Anonymous

      Take a look at my latest post on the subject: http://ohryan.ca/blog/2011/01/21/modern-mobile-redirect-using-htaccess/

      Simply remove the ‘ipad’ from the ruleset I mention there.

  10. Gazzygazet Avatar
    Gazzygazet

    Realy helpfull!!!! Thanks

  11.  Avatar

    [url=http://www.newjackets-online.com]canada goose jackets[/url]
    [url=http://www.newjackets-online.com]canada goose parka[/url]
    [url=http://www.newjackets-online.com]canada goose coats[/url]

  12.  Avatar

    http://www.newjackets-online.com/canada-goose-mens-parka-canada-goose-men-banff-c-249_256.html
    http://www.newjackets-online.com/canada-goose-mens-parka-canada-goose-trillium-parka-c-249_254.html
    http://www.newjackets-online.com/canada-goose-mens-parka-canada-goose-men-banff-c-249_256.html
    http://www.newjackets-online.com/canada-goose-mens-parka-canada-goose-men-chateau-c-249_255.html
    http://www.cheapjackets-online.com
    http://www.cheapjackets-online.com
    http://www.cheapjackets-online.com/canada-goose-womens-parka-c-247.html
    http://www.cheapjackets-online.com/canada-goose-mens-parka-canada-goose-trillium-parka-c-1_6.html
    http://www.cheapjackets-online.com/canada-goose-mens-parka-canada-goose-expedition-parka-c-1_3.html
    http://www.cheapjackets-online.com/canada-goose-mens-parka-canada-goose-men-chateau-c-1_7.html
    http://www.cheapjackets-online.com/canada-goose-mens-parka-canada-goose-men-banff-c-1_8.html
    http://www.cheapjackets-online.com/canada-goose-yorkville-parka-yellow-p-5835.html
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com
    http://www.discountparkas.com/canada-goose-mens-parka-canada-goose-men-banff-c-1_8.html
    http://www.discountparkas.com/moncler-c-258.html
    http://www.parkawholesale.com
    http://www.parkawholesale.com
    http://www.parkawholesale.com
    http://www.parkawholesale.com
    http://www.parkawholesale.com/canada-goose-men-and-women-c-263.html
    http://www.parkawholesale.com/canada-goose-womens-montebello-parka-spirit-p-5923.html
    http://www.parkawholesale.com/canada-goose-womens-parka-c-247.html
    http://www.parkawholesale.com/canada-goose-kids-c-262.html
    http://www.newparkas.com
    http://www.newparkas.com
    http://www.newparkas.com
    http://www.newparkas.com
    http://www.newparkas.com/canada-goose-mens-parka-canada-goose-chilliwack-parka-c-1_2.html
    http://www.newparkas.com/canada-goose-mens-parka-canada-goose-men-chateau-c-1_7.html
    http://www.monclerjacketscoats2011.com
    http://www.monclerjacketscoats2011.com
    http://www.monclerjacketscoats2011.com
    http://www.monclerjacketscoats2011.com
    http://www.monclerjacketscoats2011.com
    http://www.monclerjacketscoats2011.com
    http://www.monclerjacketscoats2011.com/moncler-women-jackets-c-11.html
    http://www.monclerjacketscoats2011.com/moncler-bags-c-1.html
    http://www.monclerjacketscoats2011.com/moncler-shoes-c-9.html
    http://www.monclerjacketscoats2011.com/moncler-kids-c-4.html
    http://www.monclerjacketscoats2011.com/moncler-boots-c-2.html
    http://www.monclerjacketscoats2011.com/moncler-hat-neckcloth-c-3.html
    http://www.monclerjacketscoats2011.com/moncler-kids-c-4.html
    http://www.monclerjacketscoats2011.com/moncler-hat-neckcloth-c-3.html
    http://www.monclerjacketscoats2011.com/moncler-men-sweater-c-6.html
    http://www.monclerjacketscoats2011.com/moncler-shoes-c-9.html
    http://www.monclerjacketscoats2011.com/moncler-women-jackets-c-11.html

Leave a Reply

Only people in my network can comment.