I have assemled these rules from various sources, do any of you see any pitfalls?
I'm using this to setup a mobile version of an ZendFramework powered website.
# You ofcourse need this to start the magic, and mod_rewrite enabled ;)
RewriteEngine On
# iOS redirection.
# Make sure the URL hasn't already been rewritten internally
RewriteCond %{ENV:REDIRECT_STATUS} =""
# Make sure we don't redirect links from mobile to default (web) back to mobile
RewriteCond %{HTTP_REFERER} !domain\.com
RewriteCond %{REQUEST_URI} !^/m
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-s
# Don't use this Cond if you want direct requests on 'domain.tld' go to mobile
# RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
RewriteRule ^(.*)$ /m/$1 [R,L]
# ZendFramework redirection
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteRule ^.*$ index.php [NC,L]
I'm not a mod_rewrite expert, so what does [R,L] and [NC,L] mean?
Update
Just tested this with an iPad, and it don't redirect in it. But again, i guess the mobile version would not be needed for iPads.. What do you think?
Update on the update
Hmm.. just realised that going directly to http://www.domain.com/
from iPhone don't work, it has though. Adding something to the query like http://www.domain.com/tracker
correctly redirects to http://www.domain.com/m/tracker
- So maybe this is the reason the iPad didn't show the mobile site (and i don't have the iPad at hands right now).
Third update
Had another go at the problem with mobile website not showing when going directly to http://www.domain.com/
. The RewriteCond %{REQUEST_FILENAME} !-d
was the problem.
http://detectmobilebrowser.com/ This website has a quite extensive RegEx for detecting mobile devices. I have replaced my
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
with it. The previous Cond only targets Apple devices.The complete mod_rewrite from detectmobilebrowser.com is:
I think you should spice it up with one of my Cond's above, i think it may be prone to end up in and infinite loop - havent tested.