Categories
Tips & How To's

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 replies on “Revisiting Mobile Redirection Using .htaccess ReWrite Rules”

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

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.

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.

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.

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

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

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

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.

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.

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.

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.

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.

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.

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

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.

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

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

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]

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

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…

[…] 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.