如何在web.config中添加多个URL重写规则

时间:2014-03-20 00:23:46

标签: html asp.net-mvc iis iis-7

如何在web.config中添加多个URL重写规则

我希望匹配任何包含" sales"和" registrationsuccess"。

我在尝试以下操作时遇到错误:

  <rewrite>
    <rules>
        <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
            <match url="(.*sales*)"/>
           <match url="(.*registrationsuccess*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
    </rules>
</rewrite>

1 个答案:

答案 0 :(得分:28)

这是您添加多个规则的方式:

  <rewrite>
    <rules>
        <rule name="Redirect HTTP to HTTPS (Sales)" stopProcessing="true">
            <match url="(.*sales*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
        <rule name="Redirect HTTP to HTTPS (RegistrationSucces)" stopProcessing="true">
            <match url="(.*registrationsuccess*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$"/>
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
        </rule>
    </rules>
</rewrite>

但是对于你想要做的事情,你可以用一条规则来做,如下所示:

<rule name="Redirect to HTTPS" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="ON" />
        <add input="{PATH_INFO}" pattern="^(.*)/sales/(.*)" />
        <add input="{PATH_INFO}" pattern="^(.*)/registrationsuccess/(.*)" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>