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.

Free Apple Desktop
Creative Commons License photo credit: CAVE CANEM

Here is my Apache rewrite rule that I use to

  1. check if its an iPhone, redirect to optimized version
  2. Identify the most common browsers by User Agent and redirect them to web version
  3. 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 site web.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.Roblox HackBigo Live Beans HackYUGIOH DUEL LINKS HACKPokemon Duel HackRoblox HackPixel Gun 3d HackGrowtopia HackClash Royale Hackmy cafe recipes stories hackMobile Legends HackMobile Strike Hack

This set of rules where:

  1. check if its an iPhone, redirect to optimized version
  2. check if it doesn’t support WAP, redirect to web version
  3. 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.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Type your comment here.

Hi I did an Apache Mobile Filter and I think could help you to resolve mobile (include iPhone) problem.
For more details:

http://www.idelfuschini.it/index.php/apache-mobile-filter.html

i wonder will this can solve the error “Safari could not open this page becoz too many redirects” when i go to my iphone version web site.? the iphone detection script was coded in the main web index.php which will only then redirect to my iphone-index.php..

Hi,
I did just know the Apache Mobile Filter V1.61
From version of februry I change a lot of thinks:

1) is faster
2) is more accuracy
3) easy to use
4) have alot of feature

For more details go to:

http://www.idelfuschini.it/apache-mobile-filter.html

Hi,
I realize the new Apache Filter V2.00
Read this info: http://www.idelfuschini.it/apache-mobile-filter-v2x.html

Hi! The post is really interesting! I

Some more new about Apache Mobile Filter hase involved in this time.
The last version now is V3, it’s a suite of several modules:

1. Detect correctly the device and pass to the other web application (php, ruby, perl ….. etc.) the capabilities as environment variables (AMFWURFLFilter)
2. Cluster Caching mode (AMFWURFLFilterMemcached)
3. Mobile Caching System (AMFMobileCaching)
4. Resize images on the fly to adapt to the screen size of the mobile devices (AMFImageRendering)
5. Switch the browser to a correct site (AMFSwitcher)
6. Carrier Detection (Vodafone, TIM, Orange etc. etc.) (AMFCarrierDetection)
7. Admin Tool (only for filesystem caching) (AMFDeviceMonitor)

From November 2009 the AMF project it’s included in the Apache Module Registry (http://modules.apache.org).
In this url there are some success histories:
http://www.idelfuschini.it/en/-success-histories.html

Hmm. I also tried to get this work, but when i upload this .htaccess-file my server does not show the website again. there is an error 500:
Error 500 – Internal server error.
how can i fix this? if i delete the .htaccess it works again

thanxx for your help 😉

Error 500 – Internal server error.
Any working solution?
THX

mike and juno:

Error 500 means the server did something wrong. It’s an HTTP error, which means the server is telling the browser that the server messed up.

Unfortunately, there is no further information, which makes debugging a real pain.

From experience, there’s probably an error in your rewrite rules. Ask your host where they keep the error logs that Apache and PHP produce, it might turn up some clues. Also, check that you’ve spelt everything correctly.

There are quite a big number of problems that could be causing the error, it’s hard to pinpoint which one without more information.

But good luck! Every problem has a solution…

ок : завяжу на узелок

Today, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!