Search code examples
iisurl-rewritingoutbound

Scope IIS Rewrite Outbound Rule by request URL


Is there any way to scope IIS' outbound rewrite rule to only apply if the requested URL matches a pattern? I suspect that's not possible because the request URL isn't visible in the response, but I thought I'd ask in hopes that it is possible.

Here is my current outbound rule:

<rewrite>
    <outboundRules> 
        <rule name="Change Absolute Paths" preCondition="ResponseIsHtml1"> 
            <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.mycompany.com/blog(.*)$" /> 
            <action type="Rewrite" value="https://www.mycompany.com/blog{R:2}" /> 
        </rule> 
        <preConditions> 
            <preCondition name="ResponseIsHtml1"> 
                <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> 
            </preCondition> 
        </preConditions> 
    </outboundRules> 
</rewrite>

It would be great if I can add a condition in there so that the outbound rule only applies if the request URL matches a pattern.


Solution

  • It turns out to be easy. You just need to add a condition on {REQUEST_URI}.

    <rewrite>
        <outboundRules> 
            <rule name="Change Absolute Paths" preCondition="ResponseIsHtml1"> 
                <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.mycompany.com/blog(.*)$" /> 
                <conditions>
                    <add input="{REQUEST_URI}" pattern="^/foobar" />
                </conditions>
                <action type="Rewrite" value="https://www.mycompany.com/blog{R:2}" /> 
            </rule> 
            <preConditions> 
                <preCondition name="ResponseIsHtml1"> 
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> 
                </preCondition> 
            </preConditions> 
        </outboundRules> 
    </rewrite>