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.
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.
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.
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.
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:
- 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.
- 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.
- 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With