Apache rewrite rule for iPhone users
Like so many others these days I am preparing some of my web pages for iPhone invation. I am using this Apache rewrite rule only on the top level domain to route users to their optimized version. When you visit one of these versions, they are all accessable from any device. This is also what Apple recommends.

photo credit: CAVE CANEM
Here is my Apache rewrite rule that I use to
- check if its an iPhone, redirect to optimized version
- Identify the most common browsers by User Agent and redirect them to web version
- redirect all others to mobile version
Rewrite rule
ServerName www.mydomain.no # This enables rewriting in this directory RewriteEngine On # Catch iPhone-users first, easiest to discover RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari RewriteRule ^[\./](.*)$ http://beta.mydomain.no/iphone [L] # Catch most familiar web browsers and redirect to web version, # except Opera Mini and SymbianOS (which identifies itself as Safari) RewriteCond %{HTTP_USER_AGENT} ((.*MSIE.*Windows\ NT.*)| \ (Lynx.*)|(.*Safari.*)|(.*Opera.*)|(.*Firefox.*)|(.*Konqueror.*)) RewriteCond %{HTTP_USER_AGENT} !(.*Opera\ Mini.*) RewriteCond %{HTTP_USER_AGENT} !(.*SymbianOS.*) RewriteRule ^[\./](.*)$ http://beta.mydomain.no/web [L] # Browsers that match neither block, such as regular screen # browsers, could be caught by a final rewrite rule placed here, # or we could leave it out and have nothing happen to the # requested URL. This is the default. RewriteRule ^[\./](.*)$ http://beta.mydomain.no/mobile [L]
Notice that it doesn’t sniff for “iPhone”, but “Mobile Safari”. This makes also iPod Touch compatible, which currently is the only device using the same browser.
Alternative rewrite rule
This is the old rewrite rule that I used. This should only be used as a starting grid for further development. The WAP-test is not optimal, seems like Nokia’s and Sony Ericsson’s latest browsers get identified as ordinary web browsers without WAP-support and as a result gets redirected to the web version.
This set of rules where:
- check if its an iPhone, redirect to optimized version
- check if it doesn’t support WAP, redirect to web version
- redirect all others to mobile version
ServerName mydomain.no # This enables rewriting in this directory RewriteEngine On # Catch iPhone-users first, easiest to discover RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari RewriteRule ^[\./](.*)$ http://beta.mydomain.no/iphone [L] # Catch XHTML MP browsers first and return XHTML MP content to them RewriteCond %{HTTP_ACCEPT} !text/vnd\.wap\.wml RewriteRule ^[\./](.*)$ http://beta.mydomain.no/web [L] # Browsers that match neither block, such as regular screen # browsers, could be caught by a final rewrite rule placed here, # or we could leave it out and have nothing happen to the # requested URL. This is the default. RewriteRule ^[\./](.*)$ http://beta.mydomain.no/mobile [L]
Please leave a comment if you see or have a smarter way of doing this.



Type your comment here.