简体   繁体   中英

IIS Redirect rule based on multiple (but not all) conditions

I have a rewrite that is set up to handle multiple different user agents, and I want to be able to match my rule on any of them. However, any url that matches one of these must also match another rule (an IP address). However, I can't find any documentation on how to do this. Can anyone make any suggestions on how I can do this?

Below is an example of what I'm trying to achieve. I know that this will fail because the conditions node has already been declared more than once.

So, in essence it's a redirect when any of the {HTTP_USER_AGENT} rules and any of the {REMOTE_ADDR} rules are matched.

<rule name="Mobile UA redirect" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
    <!-- Any of these can be matched -->
    <add input="{HTTP_USER_AGENT}" pattern="Android" />
    <add input="{HTTP_USER_AGENT}" pattern="BlackBerry" />
    <!-- ... more user agents... -->
  </conditions>
  <!-- Here, similarly, any one of these rules can be matched, but one of the rules above must also match one of the rules below. -->
  <conditions logicalGrouping="MatchAny">
    <add input="{REMOTE_ADDR}" pattern="127.0.0.1" />
    <add input="{REMOTE_ADDR}" pattern="192.168.0.1" />
  </conditions>
  <action type="Redirect" url="http://mob.mydomain.com/{R:0}" appendQueryString="true" />
</rule>

Any help on how I can do this would be greatly appreciated!

What about smth like the following, placed at the bottom:

<rule name="MobileRestricted" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <add input="{REMOTE_ADDR}" pattern="127.0.0.1" negate="true" />
    <add input="{REMOTE_ADDR}" pattern="192.168.0.1" negate="true" />        
  </conditions>
  <action type="None"/>
</rule>
<rule name="Mobile UA redirect" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
    <!-- Any of these can be matched -->
    <add input="{HTTP_USER_AGENT}" pattern="Android" />
    <add input="{HTTP_USER_AGENT}" pattern="BlackBerry" />
    <!-- ... more user agents... -->
  </conditions>
  <action type="Redirect" url="http://mob.mydomain.com/{R:0}" appendQueryString="true" />
</rule>

Not a single rule, but no more than two )

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