Not-Device Detection Example Code

25 01 2009

Well I guess it wasn’t “just” after xmas – been a bit busy enjoying the Australian summer!

We’ve had a lot of interest in the “Not-Device Detection” solution and lots of requests for example code so here it is.

Below is an example snippet from an httpd.conf (e.g. configuration for Apache) using mod_rewrite.  This assumes you have 3 main version of your homepage.

  1. An iPhone/iPod Touch version using /iphone/index.html
  2. A Plain Old Mobile (POM) version using /pom/index.html
  3. A standard PC version using /index.html

Of course you can apply this model to more sophisticated solutions that also single out other specific devices or more complex collections of sub-pages. Other examples of this could also be implemented using perl or php scripts, but the performance is much better at the web server configuration level.

So, for this example let’s step through the code.

First make sure you have the RewriteEngine switched “on”.
RewriteEngine   on
Then detect the iPhone/iPod Touch first since they’re so easy to identify.
# iPhone/iPod redirected to iPhone index
RewriteCond %{HTTP_USER_AGENT}  ^.*iP(hone|od)(;|\s).*$
RewriteRule     ^/$         /iphone/index.html [PT]

The line below allows a user to choose to view the PC version by adding ?pc to the URL (e.g. from a specific switcher icon)
RewriteCond %{QUERY_STRING}     !^pc$ [NC]
You may want to exclude a number of standard bots here using generic strings.
You may also want to add exclusions for specific monitoring tools etc.  here too
RewriteCond %{HTTP_USER_AGENT} !(spider|crawl|slurp|bot) [NC]
Then here’s the meat of the “Not-Device Detection” solution.  You’ll notice that it mostly consists of !^ statements that say if this browser is NOT Linux, NOT Windows (excluding Win CE), NOT OS X or OS 9, NOT Solaris and NOT BSD then we’ll assume it’s a Mobile Phone and redirect it to /pom/index.html.
# Plain Old Mobile (POM) redirection (by exclusion)
RewriteCond %{HTTP_USER_AGENT}  !^.*Linux.*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*Win.*$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT}  ^.*Windows\s+CE.*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*OS\s+(X|9).*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*Solaris.*$ [NC]
RewriteCond %{HTTP_USER_AGENT}  !^.*BSD.*$
RewriteRule     ^/$         /pom/index.html [PT]

Otherwise it should just receive /index.html as normal.

While this may look a little complex if you’re not comfortable reading regular expressions…I think you’ll agree it’s extremely simple when you compare it to this spaghetti diagram over on the .mobi forum.

I’d like to thank James for creating this work of art…now whenever I want to explain to a client why having a .mobi domain is a wrong headed and silly idea that makes your web strategy more complicated I just point them to this single diagram.

Anyway, I hope you find the example configuration described above useful. If you have any questions or would like any help implementing this please post a comment here.








Follow

Get every new post delivered to your Inbox.