Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to detect mobile users based on their user_agent.

If a user is browsing the full version of the site and they have a certain user_agent, I want to redirect them (using mod_rewrite) to the mobile site - if the mobile page exists.

Full site URL example Equivalent mobile site URL example (note .htm instead of .html)

I need to do this with apache and not something like php

Bonus: The mobile site could have links to the full site (if the page doens't have a mobile equilelant) and I want to keep redirecting people and I want to continue the redirection unless people click on a "Full Site" link which will opt them out of redirection via a url parameter - ex: ?ver=ful

share|improve this question
What're the physical file paths of both the main site and the mobile site? – Shane Madden Jan 23 '12 at 21:49
Full: /var/www/html Mobile: /var/www/mobile – ckliborn Jan 23 '12 at 22:02
up vote 1 down vote accepted

I'll have a stab.

In the full site vhost:

RewriteCond %{HTTP­_US­ER_­AGENT} Some-Mobile-UA [OR]
RewriteCond %{HTTP­_US­ER_­AGENT} Some-Other-Mobile-UA [OR]
RewriteCond %{HTTP­_US­ER_­AGENT} Yet-Another-Mobile-UA [OR]
RewriteCond %{HTTP­_US­ER_­AGENT} The-Final-Mobile-UA
RewriteCond /var/www/mobile%{REQU­EST­_URI} -f
RewriteCond %{HTTP­_CO­OKIE} !stayOnFullSite
RewriteRule /(.*)l$1 [R]

And in the mobile site vhost:

RewriteCond %{QUER­Y_S­TRING} ver=full
RewriteRule ^ - [CO=stayOnFullSite:true]

RewriteCond %{REQU­EST­_FI­LENAME} !-f
RewriteCond /var/www/html%{REQU­EST­_URI}l -f
RewriteRule /(.*)$1l [R]


I've rather crudely chopped the trailing "l" off .html when redirecting to the mobile site and added it back on when redirecting back.

I also chopped it off when checking that the file exists on the main site but I can't figure out how to remove it when checking that the file exists on the mobile site. Possibly another RewriteCond in front of it that matches all but the final "l" in %{REQUEST_URI} and then use %1 in the following RewriteCond like this:

RewriteCond %{REQU­EST­_URI} (.*)l
RewriteCond /var/www/mobile%1 -f

It might be easier to standardise your site on a single file extension.

You can set the rest of the available cookie parameters. Details are in the mod_rewrite docs and the section on flags.

This configuration is completely untested.

share|improve this answer
Thanks for taking a stab at it - would it be better to have the full site vhost set the stayOnFullSite cookie? people would never go to a mobile page with the ?ver=ful URL param – ckliborn Jan 23 '12 at 23:45
Oh yes, it would make more sense in the full site vhost. – Ladadadada Jan 23 '12 at 23:49
Awesome - can you explain a little more how this code checks to see if a page exists on the mobile site – ckliborn Jan 23 '12 at 23:57
Looks good to me - yeah, the .htm/.html thing makes it uglier than it needs to be. @ckliborn The RewriteCond /var/www/mobile%1 -f checks for file existence at the mobile site. – Shane Madden Jan 24 '12 at 1:33
How can I store {REQU­EST­_URI} in a variable, manipulate it and then use it in a RewriteCond or RewriteRule – ckliborn May 16 '12 at 14:57

Look Apache Mobile Filter is free, easy to install and to configured. Here is an example:

#Configuration AMF Filter
PerlSetEnv AMFMobileHome /usr/local/AMF
PerlSetEnv AMFProductionMode true
PerlSetEnv ServerMemCached localhost:11211

PerlTransHandler +Apache2::AMFLiteDetectionFilter
RewriteEngine on
RewriteCond %{ENV:AMF_DEVICE_IS_MOBILE} ^true*
RewriteRule ^(.*)$ [R=301,L]

Let me know

share|improve this answer
That rewrite rule should be RewriteRule ^(.*)$$1 [R=302,L]. Two reasons: 1. Always redirect to the same page on the mobile site. 2. Don't use a permanent redirect. – Ladadadada May 16 '12 at 15:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.