【问题标题】:How to add multiple URL rewrite rules in a web.config如何在 web.config 中添加多个 URL 重写规则
【发布时间】:2014-04-26 14:12:39
【问题描述】:

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

我想匹配任何包含“sales”和“registrationsuccess”的url。

当我尝试以下操作时出现错误:

  <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>

【问题讨论】:

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


    【解决方案1】:

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

      <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>
    

    【讨论】:

    • 大家好,请您指导我stackoverflow.com/questions/27148725/…
    • @TomHall,我如何将其他所有其他页面重定向到 HTTP 销售和注册成功?
    • 我认为最后一个规则逻辑有问题。由于logicalGrouping="MatchAny" 无论HTTPS是否开启都不会发生重定向?
    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2020-01-05
    • 1970-01-01
    相关资源
    最近更新 更多