【发布时间】:2017-12-10 04:21:40
【问题描述】:
我们的 webconfig 文件使用 Url Rewrite,本质上是将任何 http 流量推送到 https
除了在本地开发之外,这很好用。有一段时间我们只需要记住注释掉 web.config 中的代码并再次取消注释以进行提交。这自然不是一个好的工作方式。
代码很简单
<rewrite>
<rules>
<rule name="Redirect-AllWWW-ToSecureNonWWW">
<match url="^((?!local).)*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}"/>
</rule>
<rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost">
<match url="^((?!local).)*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>
根据 regex101 ,它可以工作! https://regex101.com/r/3Mz6w1/1
但是,在本地主机上时,我仍然被定向到 HTTPS
为什么它在 regex101 中有效,而不是在我的 web.config 文件中
【问题讨论】:
-
你需要这样的东西吗?
https?:\/\/(?!.*?local).* -
如果网站是http...链接显示我需要匹配的内容?
-
什么意思?上面的正则表达式有什么问题?
-
也许添加字符串锚的结尾:regex101.com/r/3Mz6w1/1
-
对不起@CasimiretHippolyte,我的问题不清楚。虽然我对正则表达式有一些问题,但问题更多的是为什么配置文件的规则会忽略它。我已经使用您的示例更新了我的帖子
标签: regex web-config url-rewrite-module