如何在web.config中添加多个URL重写规则 [英] How to add multiple URL rewrite rules in a web.config

查看:1283
本文介绍了如何在web.config中添加多个URL重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to add multiple URL rewrite rules in a web.config

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

I want to match any url that contains "sales" and "registrationsuccess".

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

I get errors with when I try the following:

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


推荐答案

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

This is how you add more than one rule:

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

但是对于您要做的事情,您可以使用一条规则来做到这一点,如下所示:

But for what you're trying to do, you can do it with one rule, like below:

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

这篇关于如何在web.config中添加多个URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆