简体   繁体   English

IIS 重定向规则优先级

[英]IIS redirect rule priority

If I'm using httpRedirect and rewrite in my Web.config file, how can I specify which rule takes precedence?如果我在我的 Web.config 文件中使用httpRedirectrewrite ,我如何指定哪个规则优先?

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.例如,假设我有一个wildcard="*"类的通用规则,但也有一个wildcard="/news" destination="/new/news" ,似乎只有wildcard="*"规则是执行。

It is possible to get such behavior from Apache;可以从 Apache 获得这种行为; I imagine there must be a way in IIS.我想IIS中一定有办法。

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.优先级与它们指定的顺序相同。IIS 管理器有一个“上移”和“下移”按钮,可以为您重新排序。

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 .考虑对/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 .在这个例子中,因为Rule1是第一个,这意味着请求将被重写为foo.aspx?bar=special而不是foo.aspx?bar=123 ,即使它同时匹配Rule1Rule2Rule3

The stopProcessing="true" attribute ensures that other matching rules are not executed (ie Rule2 and Rule3 ). stopProcessing="true"属性确保不执行其他匹配规则(即Rule2Rule3 )。

Source: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rules_Evaluation来源: 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. IIS 中的每个配置级别都可以定义零个或多个重写规则。 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: URL 重写模块使用以下算法处理规则集:

  1. First, the URL is matched against the pattern of a rule.首先,将 URL 与规则模式进行匹配。 If it does not match, the URL Rewrite Module immediately stops processing that rule, and goes on to the next rule.如果不匹配,URL 重写模块会立即停止处理该规则,并继续执行下一条规则。
  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.如果模式匹配并且规则没有条件,则 URL 重写模块执行为此规则指定的操作,然后继续执行下一个规则,在该规则中使用替换的 URL 作为该规则的输入。
  3. If a pattern matches and there are conditions for the rule, the URL Rewrite Module evaluates the conditions.如果模式匹配并且规则有条件,则 URL 重写模块会评估条件。 If the evaluation is successful, the specified rule action is performed, and then the rewritten URL is used as input to the subsequent rule如果评估成功,则执行指定的规则动作,然后将重写后的URL作为后续规则的输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM