【问题标题】:Rule not working in rewrite rules despite valid regex尽管有有效的正则表达式,但规则在重写规则中不起作用
【发布时间】: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


【解决方案1】:

这似乎与Redirect rule not working有关

引用URL Rewrite Module Configuration Reference

重写规则模式用于指定与当前URL路径进行比较的模式。

...

模式在重写规则的 元素中指定。

根据这条官方信息,您必须确保 &lt;match url 仅与 URL 路径 不包含主机名 比较,不是全部网址

对于 Url Rewrite Module,此问题的 URL 路径例如是 questions/44944175/rule-not-working-in-rewrite-rules-despite-valid-regex。没有stackoverflow.com 没有https:// 没有查询字符串,只有没有前导斜杠的路径。

要忽略对包含 local 的主机名的请求,您需要一些条件来寻找与 HTTP_HOST 标头匹配的 local

<rewrite>
    <rules>
        <rule name="Redirect-AllWWW-ToSecureNonWWW" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <!-- continue if http host name does not contain "local" -->
                <add input="{HTTP_HOST}" pattern="local" negate="true" />

                <add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
            </conditions>
            <action type="Redirect" url="https://{C:1}/{R:0}" />
        </rule>
        <rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <!-- continue if http host name does not contain "local" -->
                <add input="{HTTP_HOST}" pattern="local" 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>

【讨论】:

  • 非常感谢 negate="true"
猜你喜欢
  • 2016-02-10
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
相关资源
最近更新 更多