1

I'm trying to setup a mobile re-direction script in classic ASP that detects the HTTP request and if its mobile, it will redirect the request to a mobile version of that page

so if this link opened from a mobile device:

http://www.example.com/about.asp it would re-direct to http://m.example.com/about.asp

most of the script i have tried they all to a re-direct to the mobile site home page, but I need to have it re-direct to a page level.

if this is doable in IIS7.5 i'm all for it too.

I got this to work with a help,

right now i have an issue, I have few folders which I need to block them from re-directing. i have piece of code, the folders are NOT being re-directed which is okay but when I access any other pages it goes to a homepage m.example.com....not sure what I'm doing wrong here

 <rule name="Mobile Redirect" stopProcessing="true">
     <match url="^(example1|example2|exaple3)/?" ignoreCase="true" negate="true" />
      <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
        <add input="{HTTP_USER_AGENT}" pattern="^(?!.*ipad).*(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://m.example.com/{R:0}" />
    </rule>
jheul
  • 1,201
  • 3
  • 9
  • 15

1 Answers1

1

Using IIS 7.5, you could use the following rule:

<rule name="Mobile Redirect" stopProcessing="true">
    <match url="^.*$" ignoreCase="true" />
    <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://m.example.com/{R:0}" />
</rule>

url="^.*$" will match any url and redirect to http://m.example.com happening the requested path if the conditions are met.

If you want to NOT apply this rule to the iPad, we will assume that the iPad user agent is as following (the important part will be that the word iPad is in it):

Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

(Source: What is the iPad user agent?)

Then you can modify the rule to this:

<rule name="Mobile Redirect" stopProcessing="true">
    <match url="^.*$" ignoreCase="true" />
    <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
        <add input="{HTTP_USER_AGENT}" pattern="^(?!.*ipad).*(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://m.example.com/{R:0}" />
</rule>

Where pattern="^(?!.*ipad).*(midp|mobile|phone).*$" will match midp|mobile|phone only if ipad is not present. (the pattern is, by default, not case sensitive)

Community
  • 1
  • 1
cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • this worked just great, how do I limit ipad users from re-directing i dont want Ipad users to redirect, is it possible with the URLrewrite? – jheul May 31 '13 at 17:40
  • I am testing something and will update my answer if I find a solution! – cheesemacfly May 31 '13 at 18:13
  • hey thanks for your code but it didnt work, it re-directed to the mobile version. – jheul May 31 '13 at 18:41
  • What if you use `pattern="^(?!.*ipad).*(midp|mobile|phone).*$"` on all the inputs to test? Can you find the user agent of your ipad browser? – cheesemacfly May 31 '13 at 18:44
  • As a test, what if you keep only one condition: ``? – cheesemacfly May 31 '13 at 18:49
  • Thanks this worked, quick question: can I do this with specific folders too? like NOT to have them re-directed within the same rule? like an example folder /example/ – jheul May 31 '13 at 18:53
  • Not sure I understand your question :) – cheesemacfly May 31 '13 at 18:55
  • sorry my bad, so they way I have it setup with your help is to re-direct all the pages and etc to a mobile version, so what if i have a folder called http://www.example.com/example/ and i DON'T want to have that folder re-directed to a mobile version, can I put a block within the same rule? hope this makes more sense. – jheul May 31 '13 at 18:59
  • In this case, simply change `` to ``. So if the requested path starts with `example` or `example/`, the rule won't be triggered. – cheesemacfly May 31 '13 at 19:04
  • i see makes sense, and what if i have multiple folders? – jheul May 31 '13 at 19:06
  • Then you can use the `|` to separate the different folders. For example: `` would match any of the 3 words blog, about or home – cheesemacfly May 31 '13 at 19:09
  • Hi, sorry i one issue with outbound rewrite...do need to set it within the same config file? – jheul Jun 03 '13 at 13:28
  • hi there's an issue with this piece, it wont stop the re-direct it just goes to m.example.com homepage `code``code` – jheul Jun 03 '13 at 14:03
  • What is the full configuration? If it is a different problem, it may be worth asking a new question :) – cheesemacfly Jun 03 '13 at 14:25
  • the configuration is the same as it was discussed few days ago, when i added the folder to it did't stop the re-direct it goes to the mobile homepage. m.example.com – jheul Jun 03 '13 at 14:41
  • i dont think i need to ask a new questions? :) do I? – jheul Jun 03 '13 at 15:10
  • What url are you trying to reach? Because you have the `negate="true"` so it means whatever url not starting with `example` will trigger the rule! – cheesemacfly Jun 03 '13 at 15:15
  • im trying to reach this URL /example/ - if this URL is accessed DO NOT redirect. – jheul Jun 03 '13 at 15:21
  • or even any other folder i get redirected to the homepage again. – jheul Jun 03 '13 at 15:46
  • That is weird if `http://yourwebserver/example` triggers the rule. Are you sure your browser hasn't cached the redirect (when it's a 301 permanent, some browsers do it). If not, can you use the [`failed request tracing tool`](http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules) to see where it gets triggered? – cheesemacfly Jun 03 '13 at 15:53
  • I cleared the Cache. I used this code below the folders are not being re-directed which is good, but when i access everything else it goes to the mobile homepage – jheul Jun 03 '13 at 16:25
  • I don't get it :) This is the goal here no? If not, please edit your question with the full rule and problem, it will be easier than in the comments. – cheesemacfly Jun 03 '13 at 16:41