简体   繁体   中英

URL Rewrite multiple AND conditional redirect

I am looking for a URL Rewrite rule to redirect any request made to www.XYZ.com , inte.XYZ.com, prep.XYZ.com, and prod.XYZ.com from the list of IP su.nets below to redirect to https://test.XYZ.com .

111.000.111.0/24

111.111.222.0/22

111.222.333.0/24

I have written the rule as below:

<rule name="RedirectToXYZTest" stopProcessing="true">
   <match url=".*" />
   <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^www\.XYZ\.com$|^inte\.XYZ\.com$|^prep\.xyz\.com$|^prod\.xyz\.com$" />
      <add input="{REMOTE_ADDR}" pattern="^111\.000\.111\.\d{1,3}$" />
      <add input="{REMOTE_ADDR}" pattern="^111\.111\.222\.\d{1,3}$" />
      <add input="{REMOTE_ADDR}" pattern="^111\.222\.333\.\d{1,3}$" />
   </conditions>
   <action type="Redirect" url="https://test.xyz.com/" />
</rule>

The issue with the above is that the condition works even with any one condition because of the matchany logical condition. I need an AND condition of Http_Host && one of the Ip address to perform the redirect action.

ie if the url is www.xyz.com ,any of the Ip address range in the given regex like 111.000.111.3 should fullfil the criteria to redirect.

Cant find any precondition for inbound rule.

The first idea comes to my mind is to define three different rules using MatchAll :

<rules>
   <rule name="RedirectToXYZTest1" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
         <add input="{HTTP_HOST}" pattern="^www\.XYZ\.com$|^inte\.XYZ\.com$|^prep\.xyz\.com$|^prod\.xyz\.com$" />
         <add input="{REMOTE_ADDR}" pattern="^111\.000\.111\.\d{1,3}$" />
      </conditions>
      <action type="Redirect" url="https://test.xyz.com/" />
   </rule>
   <rule name="RedirectToXYZTest2" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
         <add input="{HTTP_HOST}" pattern="^www\.XYZ\.com$|^inte\.XYZ\.com$|^prep\.xyz\.com$|^prod\.xyz\.com$" />
         <add input="{REMOTE_ADDR}" pattern="^111\.111\.222\.\d{1,3}$" />
      </conditions>
      <action type="Redirect" url="https://test.xyz.com/" />
   </rule>
   <rule name="RedirectToXYZTest3" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
         <add input="{HTTP_HOST}" pattern="^www\.XYZ\.com$|^inte\.XYZ\.com$|^prep\.xyz\.com$|^prod\.xyz\.com$" />
         <add input="{REMOTE_ADDR}" pattern="^111\.222\.333\.\d{1,3}$" />
      </conditions>
      <action type="Redirect" url="https://test.xyz.com/" />
   </rule>
</rules>

Below is the consolidated regex pattern that i have used. The default logicalGrouping is MatchAll which will verify for http_Host && Remote Addr Note: REMOTE_ADDR will only work locally & so it is good from testing the rule locally.Once deployed, it will not contain the real IP of the visitor. The rule needs to be for HTTP_True_Client_IP instead of Remote_ADDR for production.

<rule name="RedirectToXYZTest" stopProcessing="true">
   <match url=".*" />
   <conditions>
      <add input="{HTTP_HOST}" pattern="^www\.XYZ\.com$|^inte\.XYZ\.com$|^prep\.xyz\.com$|^prod\.xyz\.com$" />
      <add input="{REMOTE_ADDR}" pattern="^111\.000\.111\.\d{1,3}$|^111\.111\.222\.\d{1,3}$^111\.222\.333\.\d{1,3}$" />
   </conditions>
   <action type="Redirect" url="https://test.xyz.com/" />
</rule>

Regards.

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