jheul
jheul

Reputation: 1201

URL rewrite and Re-direct or Classic ASP script to redirect to a mobile site

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>

Upvotes: 1

Views: 1616

Answers (1)

cheesemacfly
cheesemacfly

Reputation: 11762

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)

Upvotes: 1

Related Questions