Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS redirect rule priority

If I'm using httpRedirect and rewrite in my Web.config file, how can I specify which rule takes precedence?

For instance, let's say I have a general catch all rule like wildcard="*", but also have a wildcard="/news" destination="/new/news", it seems like only the wildcard="*" rule is executed.

It is possible to get such behavior from Apache; I imagine there must be a way in IIS.

like image 503
tau Avatar asked Sep 21 '13 00:09

tau


People also ask

What is stopProcessing true?

The stopProcessing="true" attribute ensures that other matching rules are not executed (i.e. Rule2 and Rule3 ). Each configuration level in IIS can have zero or more rewrite rules defined. The rules are evaluated in the same order in which they are specified.

How do I redirect a URL to another URL in IIS?

In the Home pane, double-click HTTP Redirect. In the HTTP Redirect pane, check the box to redirect requests and enter the destination URL. You can optionally specify any of the following options: Configure the redirection destination to be the exact destination as entered.

How does IIS URL Rewrite work?

The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request. The handler, which is chosen based on the rewritten URL, processes the request and generates a response that is sent back to the Web browser.


1 Answers

Precedence is the same as the order they're specified in. IIS Manager has a "Move up" and "Move down" button that re-orders them for you.

For example:

<?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 an incoming request for /foo?bar=123.

In this example, because Rule1 is first, it means that the request will be rewritten to foo.aspx?bar=special instead of foo.aspx?bar=123, even though it simultaneously matches Rule1, Rule2, and Rule3.

The stopProcessing="true" attribute ensures that other matching rules are not executed (i.e. 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 defined. The rules are evaluated in the same order in which they are specified. The URL Rewrite Module processes the set of rules by using the following algorithm:

  1. First, the URL is matched against the pattern of a rule. If it does not match, the URL Rewrite Module immediately stops processing that rule, and goes on to the next rule.
  2. If a pattern matches and there are no conditions for the rule, the URL Rewrite Module performs the action specified for this rule and then goes on to the next rule, where it uses the substituted URL as an input for that rule.
  3. If a pattern matches and there are conditions for the rule, the URL Rewrite Module evaluates the conditions. If the evaluation is successful, the specified rule action is performed, and then the rewritten URL is used as input to the subsequent rule
like image 62
Dai Avatar answered Oct 16 '22 05:10

Dai