gpt4 book ai didi

asp.net - web.config 中的 "non-www to www"和 "https"重写规则,但不是 localhost ASP.NET MVC

转载 作者:行者123 更新时间:2023-12-04 00:10:28 27 4
gpt4 key购买 nike

我的 ASP.NET MVC 5 项目的 web.config 中有以下重写规则:

<rule name="Redirect example.com to www.example.com and enforce https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

该规则将非 www 重定向到 www,将 http 重定向到 https(因此 http://example.com/hey 将重定向到 https://www.example.com/hey) 并且工作正常。然而,它也适用于 localhost,我似乎无法解决它——我已经尝试过否定规则和包含 | 的正则表达式,但不能似乎能够找到正确的组合。我是不是以错误的方式处理这个问题?

最佳答案

conditions block 中,您可以使用属性negate="true"。如果设置了此属性并且条件匹配,则不应用重写规则。

negate 属性的描述来自IIS.net :

A pattern can be negated by using the negate attribute of the element. When this attribute is used, the rule action is performed only if the current URL does not match the specified pattern.

由于您使用的是 MatchAny,因此添加额外的属性将不会匹配,因为无论如何至少会满足一个条件。我建议在 logicalGrouping="MatchAll" 中使用 2 个特定的重写规则,其中每个规则只负责一个案例:

<rule name="enforce https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" matchType="Pattern"
pattern="^localhost(:\d+)?$" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect example.com to www.example.com"
enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTP_HOST}" matchType="Pattern"
pattern="^localhost(:\d+)?$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}"
appendQueryString="true" redirectType="Permanent" />
</rule>

关于asp.net - web.config 中的 "non-www to www"和 "https"重写规则,但不是 localhost ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707580/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com