简体   繁体   中英

IIS 7.5 URL Rewrite rule to handle request based on user agent

I have written a rule to redirect request based on user agent. The rule is set to redirect default requests (not mobile) to Domain1 and requests from mobile devices to a mobile domain, Domain2 .

Even after applying the mobile redirect, all requests from mobile are taken to Domain1 .

See the redirect rule below. Can anyone tell me what I'm missing?

<rewrite>
  <rules>
    <rule name="Mobile UA redirect" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_USER_AGENT}" pattern="^.*BlackBerry.*$ " />
        <add input="{HTTP_USER_AGENT}" pattern=".*Mobile.*Safari" />
      </conditions>
      <action type="Redirect" url="Domain2" />
    </rule>
    <rule name="Claritinchallenge to" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <action type="Redirect" url="Domain1" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

In your Mobile UA redirect rule, the conditions logical grouping is the one by default: MatchAll

I don't think a phone having a HTTP_USER_AGENT matching ^.*BlackBerry.*$ will also match .*Mobile.*Safari . So you need to change the logical grouping to MatchAny .

You rule would then be:

<rule name="Mobile UA redirect" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_USER_AGENT}" pattern="^.*BlackBerry.*$ " />
    <add input="{HTTP_USER_AGENT}" pattern=".*Mobile.*Safari" />
  </conditions>
  <action type="Redirect" url="MobileURL" />
</rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM