I recently had a need for writing a rewrite rule for mobile (cell) phone devices.
It is getting more common now for large sites to cater for the growing amounts of the mobile/cell traffic in different ways. In the organisation I am with at the moment they had a completely separate site for all the mobile users where they could download apps etc.
So I went about creating a rule for redirecting all mobile content from our main sites.
Most of our traffic to our busy sites is UK centric so there might be slight variations in other region and traffic patterns I don't know how well this will translate into other regions but I think this is a very good start. It capture 99.9+% of mobile traffic for me.
There will always be exceptions and the standards for mobile browsers is next to non existence but these rules seem to catch nearly all the mobile traffic.
The way I approached this was to look at the User Agent string in the request. This tell me about the browser the mobiles are using....well sometimes. Google user agent strings of mobile and you will see a myriad of options also some of this data is old and not a true representation of newer mobile devices.
So I first collected all the useragents I could. I wrote some Log Parser rules and went through GBs worth of logs. I found the following.
Most of the Mobile devices User Agent strings contained the following unique terms that didn't appear for desktop browsers.
MIDP (http://en.wikipedia.org/wiki/Mobile_Information_Device_Profile)
or
mobile
or
phone
(sometimes the case can vary when these terms are used)
So the rule was created around these terms. Most mobile manufacturers seemed to follow these terms (I would be interested in seeing any widely used mobiles that don't use these terms)
So that was a great basis for a rewrite rule. However it wasn't as easy as that some mobile/cell operators like Vodafone decide sometimes to route there traffic through a proxy. When routed through this proxy it changes the useragent string to a generic desktop browser looking one. Obviously a complete pain luckily they send an additional header with original user agent under the user defined X field X-Device-User-Agent so we check that too for the same terms.
Also the growing popularity of opera mini/mobile as a browser has again a more generic user agent that it is indistinguishable from a desktop browser (we really don't want valid desktop traffic going to the mobile only content site). Luckily they also send another custom X header along with the request with the original user agent the phone uses this isX-OperaMini-Phone-UA. We also check this too.
Sorry for the long explanation but I thought it was needed for explaining about mobile devices and how the networks can fit together and the pitfall you get.
Anyway here is rule just drop it into your web.config under <configuration><system.webServer> :
<rule name="Mobile Rewrite" patternSyntax="ECMAScript" stopProcessing="true"><match url=".*" ignoreCase="true" negate="false" /><conditions logicalGrouping="MatchAny" trackAllCaptures="false"><add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" /><add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" /><add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" /></conditions><action type="Redirect" url="http://mysite.mobi" appendQueryString="false" redirectType="Found" /></rule>
Where the Redirect in this case is going to http://mysite.mobi so you will have to change this to the site/directory you require.
I hope you find this useful and if you have any questions/suggestions about this or just find it useful then just drop them here in this thread.