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.
(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 is
X-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> :
thanks for that excellent sample!!! one question though, what if i wanted the users to be able to view the full version of a page despite the redirect? so on my mobile version, i want to include a link "view full version" without getting them redirected back
again ...
This works for the initial load of the full web site, but what happens when a user clicks on a link within the full site. It would revert back to the mobile site correct? Since the querystring wouldn't be in the link clicked.
I'm struggling with this right now. I can get the device detection working and the redirection with no problems. But I'm not sure how to persist the querystring once the user clicks a link in the full web site.
After some thought I am not sure it can be done effectively.
Maybe coding it could be done (something based on your session) but not ideal.
However you could have an additional subdomain for the main normal site which you could redirect to like www1.mysite.com and this never autorediects based on the mobile useragent. Once on that domain for the main site as long as nothing is hardcoded for
paths back to www.mysite.com (it shouldn't be in a modern site but no accounting for devs ;) ) you will stay on it.
So you have a condition to ignore the rule if www1. or whatever in the domain.
And in your hostheader/ Bindings have:
www.mysite.com
mysite.com'
and additionally
www1.mysite.com
Not perfect but the best I can think of. just have to get the business to prescribe to having www1.mysite.com (or whatever) for peopel going to the site.
If you really want desktop/non-mobile traffic never going to www1.mysite.com you could have a similar url redirect (based on the above rule) it to the regular www.mysite.com
Hope that makes sense and is a solution to some and something I think I will try if needed.
After some thought I am not sure it can be done effectively.
Maybe coding it could be done (something based on your session) but not ideal.
However you could have an additional subdomain for the main normal site which you could redirect to like www1.mysite.com and this never autorediects based on the mobile useragent. Once on that domain for the main site as long as nothing is hardcoded for
paths back to www.mysite.com (it shouldn't be in a modern site but no accounting for devs ;) ) you will stay on it.
So you have a condition to ignore the rule if www1. or whatever in the domain.
And in your hostheader/ Bindings have:
www.mysite.com
mysite.com'
and additionally
www1.mysite.com
Not perfect but the best I can think of. just have to get the business to prescribe to having www1.mysite.com (or whatever) for peopel going to the site.
If you really want desktop/non-mobile traffic never going to www1.mysite.com you could have a similar url redirect (based on the above rule) it to the regular www.mysite.com
Hope that makes sense and is a solution to some and something I think I will try if needed.
Hello and thank you for your reply!
Our setup has www.mysite.com and m.mysite.com and the IIS rules (mobile device redirection to m.mysite.com) are setup on the www site. To resolve the need for a "View Full Site Link" on the mobile site I setup an outbound rule that detects if the querystring
matches fullsite=1. If it does then it rewrites all the href properties within anchor tags to include the fullsite=1 querystring. Here's the rule that I'm using:
So if a user clicks the View Full Site link (which points to www.mysite.com?fullsite=1) it goes to the inbound rules (which I added a to bypass the mobile redirection if the querystring contains fullsite=1), which renders the site with the fullsite=1 in
the url and then the above outbound rule kicks in (rewriting the anchor tags with the fullsite=1 in the querystring). If there is no querystring in the href property will contain a trailing & but that doesn't effect anything. If you find a way to remove that
let me know.
I hope this helps anyone that has a similar problem.
That sounds like it would work. Any idea what regex could be used for that? I'm anything but proficient with regex. They're are two scenarios the regex would need to consider:
http://m.mysite.com/default.aspx?fullsite=1&
Rule should remove the tailing &
http://m.mysite.com/default.aspx?fullsite=1&myqsvar=1234
Rule should not remove the & since it's not trailing.
So just removing the last instance of the & wouldn't work. Any help would be appreciated!
Rovastar
3324 Posts
MVP
Moderator
URL rewrite for Mobile/Cell phone devices
Jul 22, 2010 10:10 AM|LINK
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 is X-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.
Leo Tang - M...
4161 Posts
Microsoft
Re: URL rewrite for Mobile/Cell phone devices
Jul 27, 2010 08:32 AM|LINK
Hi,
Thanks for share, Rovastar!
Feedback to us
Develop and promote your apps in Windows Store
TomTom1234
1 Post
Re: URL rewrite for Mobile/Cell phone devices
Aug 11, 2011 09:48 AM|LINK
Rovastar
3324 Posts
MVP
Moderator
Re: URL rewrite for Mobile/Cell phone devices
Aug 11, 2011 11:42 PM|LINK
I hadn't thought of that scenario when I created this rule. I'll give it some thought.....not sure how to "override" a redirect though
mcastells
1 Post
Re: URL rewrite for Mobile/Cell phone devices
Sep 27, 2011 11:16 PM|LINK
I think you can just add another condition with negate="true", for example
<add input="{QUERY_STRING}" pattern="desktop=1" negate="true" />
then add ?desktop=1 onto the link.
mikeberlin
7 Posts
Re: URL rewrite for Mobile/Cell phone devices
Nov 16, 2011 06:10 PM|LINK
This works for the initial load of the full web site, but what happens when a user clicks on a link within the full site. It would revert back to the mobile site correct? Since the querystring wouldn't be in the link clicked.
I'm struggling with this right now. I can get the device detection working and the redirection with no problems. But I'm not sure how to persist the querystring once the user clicks a link in the full web site.
Any help would be appreciated!
Rovastar
3324 Posts
MVP
Moderator
Re: URL rewrite for Mobile/Cell phone devices
Nov 17, 2011 09:20 PM|LINK
After some thought I am not sure it can be done effectively.
Maybe coding it could be done (something based on your session) but not ideal.
However you could have an additional subdomain for the main normal site which you could redirect to like www1.mysite.com and this never autorediects based on the mobile useragent. Once on that domain for the main site as long as nothing is hardcoded for paths back to www.mysite.com (it shouldn't be in a modern site but no accounting for devs ;) ) you will stay on it.
So you have a condition to ignore the rule if www1. or whatever in the domain.
And in your hostheader/ Bindings have:
www.mysite.com
mysite.com'
and additionally
www1.mysite.com
Not perfect but the best I can think of. just have to get the business to prescribe to having www1.mysite.com (or whatever) for peopel going to the site.
If you really want desktop/non-mobile traffic never going to www1.mysite.com you could have a similar url redirect (based on the above rule) it to the regular www.mysite.com
Hope that makes sense and is a solution to some and something I think I will try if needed.
mikeberlin
7 Posts
Re: URL rewrite for Mobile/Cell phone devices
Nov 17, 2011 09:38 PM|LINK
Hello and thank you for your reply!
Our setup has www.mysite.com and m.mysite.com and the IIS rules (mobile device redirection to m.mysite.com) are setup on the www site. To resolve the need for a "View Full Site Link" on the mobile site I setup an outbound rule that detects if the querystring matches fullsite=1. If it does then it rewrites all the href properties within anchor tags to include the fullsite=1 querystring. Here's the rule that I'm using:
<outboundRules>
<rule name="Persist fullsite qs var" preCondition="IsHTML">
<match filterByTags="A" pattern="^(.+?)(\?(.+))*$" />
<action type="Rewrite" value="{R:1}?fullsite=1&{R:3}" />
<conditions>
<add input="{QUERY_STRING}" pattern="fullsite=1" />
</conditions>
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
So if a user clicks the View Full Site link (which points to www.mysite.com?fullsite=1) it goes to the inbound rules (which I added a to bypass the mobile redirection if the querystring contains fullsite=1), which renders the site with the fullsite=1 in the url and then the above outbound rule kicks in (rewriting the anchor tags with the fullsite=1 in the querystring). If there is no querystring in the href property will contain a trailing & but that doesn't effect anything. If you find a way to remove that let me know.
I hope this helps anyone that has a similar problem.
Rovastar
3324 Posts
MVP
Moderator
Re: URL rewrite for Mobile/Cell phone devices
Nov 17, 2011 10:21 PM|LINK
Nice solution. I like the outbound rule idea. We have a few ways of doing it now.
What about another rule to rewrite anything with /& (and nothing else) to remove the &
so www.mysite.com/? rewrites to www.mysite.com
mikeberlin
7 Posts
Re: URL rewrite for Mobile/Cell phone devices
Nov 18, 2011 02:58 PM|LINK
That sounds like it would work. Any idea what regex could be used for that? I'm anything but proficient with regex. They're are two scenarios the regex would need to consider:
http://m.mysite.com/default.aspx?fullsite=1&
Rule should remove the tailing &
http://m.mysite.com/default.aspx?fullsite=1&myqsvar=1234
Rule should not remove the & since it's not trailing.
So just removing the last instance of the & wouldn't work. Any help would be appreciated!