We are excited to announce that the IIS.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

What determines the order of the execution of the rulesRSS

2 replies

Last post Feb 13, 2020 09:34 AM by Yuk Ding

  • What determines the order of the execution of the rules

    Feb 12, 2020 03:03 PM|amol.mahul|LINK

    The application has the following Rewrite rules

    <rules>
    <rule name="no path" stopProcessing="true">
    <match url="(.*)"/>
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{URL}" pattern="^(/)?$"/>
    </conditions>
    <action type="Redirect" url="https://www.example.se/privat/"/>
    </rule>

    <rule name="missing trailing /privat" stopProcessing="true">
    <match url="(.*)"/>
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_URI}" pattern="^/privat$"/>
    </conditions>
    <action type="Redirect" url="https://www.example.se/privat/"/>
    </rule>

    <rule name="missing www" stopProcessing="true">
    <match url="(.*)"/>
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTP_HOST}" pattern="^example.se$"/>
    </conditions>
    <action type="Redirect" url="https://www.example.se{REQUEST_URI}"/>
    </rule>

    <rule name="not https" stopProcessing="true">
    <match url="(.*)"/>
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://www.example.se{REQUEST_URI}"/>
    </rule>
    </rules>

    However the order of the rules does not have any http to http redirect, still the following order is observed


    301 http://example.se/ 
    301 http://www.example.se/ 
    200 https://www.example.se/privat/

    Can anybody assist what determines the order of rules

  • Rovastar Rovastar

    5495 Posts

    MVP

    Moderator

    Re: What determines the order of the execution of the rules

    Feb 12, 2020 03:20 PM|Rovastar|LINK

    The best way to follow this is to follow failed request tracing

    https://forums.iis.net/t/1193146.aspx?Rule+not+working+as+expected+Use+Failed+Request+Tracing

    That is if it hitting your server at all for the first first request. (301 are often cached on the client for a long time or it could be another provider in the way where you have a redirect)

    If it is hitting your server also look at server level rewrite rules

    Troubleshoot IIS in style
    https://www.leansentry.com/
  • Re: What determines the order of the execution of the rules

    Feb 13, 2020 09:34 AM|Yuk Ding|LINK

    Hi amol.mahui,

    IIS URL rewrite module will match the rule one by one from top to the bottom. If the request trigger one rule, IIS return301.  The redirected 301 rule also need to pass through the rewrite rule process again. Until all rules don't match your request url.

    Your redirection works fine on my side. "no path" redirect the URL directly.

    If I modify the order like:

    Then the redirection would looks like your description

    Since you are using 301 redirection, please try to clean your browser cache and recycle applicationpool.

    Anyway, FRT log will tell us why url get redirected and you can view the order list.

    https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

    If the reply is helpful, it is appreciated if you could mark it as answer.

    Best Regards,

    Jokies Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.