IIS Redirect Rule Priority

If I use httpRedirect and rewrite in my Web.config file, how can I indicate which rule takes precedence?

For example, suppose I have a general catch rule, like wildcard="*" , but also have wildcard="/news" destination="/new/news" , it seems that only the wildcard="*" rule is executed.

This behavior can be obtained from Apache; I suppose there should be a way in IIS.

+6
source share
1 answer

Priority is the same as the order in which they are indicated. IIS Manager has a Move Up and Move Down button that reorders them for you.

For instance:

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Rule1" stopProcessing="true"> <match url="^foo/?bar=123"/> <action type="Rewrite" url="foo.aspx?bar=special" appendQueryString="false" /> </rule> <rule name="Rule2" stopProcessing="true"> <match url="^foo/?bar=([A-z0-9]+)"/> <action type="Rewrite" url="foo.aspx?bar={R:1}" appendQueryString="false" /> </rule> <rule name="Rule3" stopProcessing="true"> <match url="^foo/"/> <action type="Rewrite" url="somethingElse.aspx" appendQueryString="false" /> </rule> </rules> </rewrite> </system.webServer> </configuration> 

Consider the incoming request for /foo?bar=123 .

In this example, since Rule1 is the first, this means that the request will be rewritten to foo.aspx?bar=special instead of foo.aspx?bar=123 , although it is the same as Rule1 , Rule2 and Rule3 .

The stopProcessing="true" attribute ensures that no other matching rules are executed (ie, Rule2 and Rule3 ).

Source: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rules_Evaluation

Each configuration level in IIS can have zero or more rewrite rules. Rules are evaluated in the same order in which they are indicated . The Rewrite URL module processes the rule set using the following algorithm:

  • First, the URL maps to the rule template. If it does not match, the URL rewrite module immediately stops processing this rule and proceeds to the next rule.
  • If the pattern matches and there are no conditions for this rule, the Rewrite module of the URL performs the action specified for this rule, and then proceeds to the next rule, where it uses the substituted URL as an input for this rule.
  • If the template matches and the conditions for the rule exist, the URL rewrite module evaluates the conditions. If the assessment is successful, the specified action of the rule is performed, and then the rewritten URL is used as an input to the next rule.
+7
source

Source: https://habr.com/ru/post/954301/


All Articles