Search code examples
iisurl-rewritingcorsurl-rewrite-module

Priority for outboundRules for URL rewrite module


Will rule #2 get hit if rule #1 does first? Or will it stop at rule #1 for IIS Url Rewrite Module. I am trying to skip webfonts with the specific origin.

<outboundRules>
        <rule name="Set Access-Control-Allow-Origin header">
          <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
            <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?(thestatbook\.com|localhost:3000)))" />
          </conditions>
          <action type="Rewrite" value="{C:0}" />
        </rule>
    <rule name="Enable CORS for Fonts">
          <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^[^\?]+\.(ttf|otf|eot|woff|woff2|svg)(\?.*)?$" />
          </conditions>
          <action type="Rewrite" value="*" />
        </rule>
      </outboundRules>

Solution

  • Will rule #2 get hit if rule #1 does first? Or will it stop at rule #1 for IIS Url Rewrite Module.

    As far as I know, both outboundRules wil hit. It will firstly run "Set Access-Control-Allow-Origin header", then "Enable CORS for Fonts". You could write a simple rule to and use postman to test it.

    Rule like below:

    This rule will modify the Access-Control-Allow-Origin and Server variable.

            <outboundRules>
          <rule name="removingserverheader" enabled="true" stopProcessing="true">
            <match serverVariable="RESPONSE_SERVER" pattern=".*" />
            <action type="Rewrite" value="0" />
          </rule>
    
          <rule name="Enable CORS for Fonts">
            <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
            <conditions>
              <add input="{HTTP_ORIGIN}" pattern=".*" />
            </conditions>
            <action type="Rewrite" value="2" />
          </rule>
        </outboundRules>
    

    Result:

    enter image description here

    If you just want only one rule is fired not hit another rule. I suggest you could try to use StopProcessing flag.

    It means when the rule action is performed (i.e. the rule matched) and this flag is turned on, it means that no more subsequent rules will be processed and the request will be passed to the IIS request pipeline. By default, this flag is turned off.

    Rule like below:

          <rewrite>
        <outboundRules>
          <rule name="removingserverheader" enabled="true" stopProcessing="true">
            <match serverVariable="RESPONSE_SERVER" pattern=".*" />
            <action type="Rewrite" value="0" />
          </rule>
          <rule name="Enable CORS for Fonts" enabled="true">
            <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
            <conditions>
              <add input="{HTTP_ORIGIN}" pattern=".*" />
            </conditions>
            <action type="Rewrite" value="2" />
          </rule>
      </outboundRules>
      </rewrite>
    

    Result:

    enter image description here