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
I'll have a stab.
In the full site vhost:
And in the mobile site vhost:
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: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.
Look Apache Mobile Filter is free, easy to install and to configured. Here is an example:
Let me know