4

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 http://www.domain.com/page1.html Equivalent mobile site URL example (note .htm instead of .html) http://m.domain.com/page1.htm

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

ckliborn
  • 2,778
  • 4
  • 25
  • 37

2 Answers2

2

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 http://m.domain.com/$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 /(.*) http://www.domain.com/$1l [R]

Notes:

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.

Shane Madden
  • 114,520
  • 13
  • 181
  • 251
Ladadadada
  • 26,337
  • 7
  • 59
  • 90
  • 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 01: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
  • Doing something like that in Apache configuration sounds extremely messy. It depends a bit on what you mean by "manipulate" but anything other than "select a substring of" is going to be difficult. You might be far better off doing this with PHP. – Ladadadada May 16 '12 at 15:03
1

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 ^(.*)$ http://m.foo.org [R=301,L]

Let me know

Idel
  • 36
  • 4
  • 1
    That rewrite rule should be `RewriteRule ^(.*)$ http://m.foo.org$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