"non-www to www" 和 "https" 重写 web.config 中的规则,但不是本地主机 ASP.NET MVC
"non-www to www" and "https" rewrite rule in web.config but not localhost ASP.NET MVC
我的 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
块中,您可以使用属性 negate="true"
。如果设置了此属性并且条件匹配,则不应用重写规则。
来自 IIS.net 的 negate
属性的描述:
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 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
块中,您可以使用属性 negate="true"
。如果设置了此属性并且条件匹配,则不应用重写规则。
来自 IIS.net 的 negate
属性的描述:
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>