【问题标题】:Regex to match all https URLs except a certain path正则表达式匹配除特定路径之外的所有 https URL
【发布时间】:2017-12-21 17:43:30
【问题描述】:

我需要一个匹配除特定路径之外的所有 https URL 的正则表达式。

例如

匹配

https://www.domain.com/blog https://www.domain.com

不匹配

https://www.domain.com/forms/*

这是我目前所拥有的:

<rule name="Redirect from HTTPS to HTTP excluding /forms" enabled="true" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{URL}" pattern="^https://[^/]+(/(?!(forms/|forms$)).*)?$" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>

但它不起作用

【问题讨论】:

  • 匹配的 url 已被询问了数百次。但是让我们忘记搜索重复项,请告诉我们您使用的是什么口味?
  • 嗨 Hamza,我试图找到一个符合我要求的正则表达式,但找不到。我正在使用 IIS URL 重写模块。规则是匹配所有除了,这是一个棘手的位。如果你能帮忙就太好了,它会让我睡几个小时。这让我很接近但不够接近stackoverflow.com/questions/2277340/…
  • 什么应该阻止完全匹配,无效 URL 的规则是什么?
  • 我想捕获所有 https 流量并将其重定向到 http,除非 https url 是 domain.com/forms*
  • 您的情况似乎有误,您的模式显示“http?”而不是“https”。可能是这个问题吗?

标签: regex iis url-rewriting


【解决方案1】:

重定向模块的工作方式,你应该简单地使用:

<rule name="Redirect from HTTPS to HTTP excluding /forms" stopProcessing="true">
    <match url="^forms/?" negate="true" />
    <conditions>
        <add input="{HTTPS}" pattern="^ON$" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" />
</rule>

仅当请求是 HTTPS 并且路径不是以 forms/forms 开头(使用 negate="true" 选项)时,该规则才会触发重定向到 HTTP。
您还可以添加主机匹配www.example.com 的条件,如下所示:

<rule name="Redirect from HTTPS to HTTP excluding /forms" stopProcessing="true">
    <match url="^forms/?" negate="true" />
    <conditions>
        <add input="{HTTPS}" pattern="^ON$" />
        <add input="{HTTP_HOST}" pattern="^www.example.com$" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" />
</rule>

【讨论】:

    【解决方案2】:

    这是否为您提供了您正在寻找的行为?

    https?://[^/]+($|/(?!forms)/?.*$)

    www.domain.com 位之后,它正在寻找字符串的结尾,或者是一个斜线,然后是不是forms 的东西。

    【讨论】:

      【解决方案3】:

      我想出了以下模式:^https://[^/]+(/(?!form/|form$).*)?$

      说明:

      • ^ : 匹配字符串开头
      • https:// :匹配 https://
      • [^/]+ :匹配除正斜杠之外的任何内容一次或多次
      • ( : 开始匹配组 1
        • / :匹配 /
        • (?!:负前瞻
          • form/ : 检查是否没有form/
          • |:或
          • form$ :检查字符串末尾是否没有form
        • ) : 结束负前瞻
        • .* :匹配所有内容零次或多次
      • ) : 结束匹配组 1
      • ? : 将前一个令牌设为可选
      • $ : 匹配行尾

      【讨论】:

      • @Sniffer 你在哪里以及如何测试它的?
      • 我使用 Expresso 对其进行了测试,并提供了不应匹配但 Expresso 表示有效的 URL 作为输入。
      • @Sniffer 在发布之前,我也对其进行了测试。 demo1demo2.
      • 这真的很奇怪,因为我测试了其他答案正则表达式并且它有效但你的没有。
      • @Burt 很简单,去掉? :p
      【解决方案4】:

      我在发布的模式http://[^/]+($|/(?!forms)/?.*$)中看到两个问题

      • 它会错过重定向 URL,例如 https://domain.com/forms_instructions,因为该模式也无法匹配那些。

      • 我相信您已经在模式和 URL 之间颠倒了 http 和 https。该模式应该有https 和URL http

      也许这会如你所愿:

       <rule name="Redirect from HTTPS to HTTP excluding /forms" enabled="true" stopProcessing="true">
              <match url="^https://[^/]+(/(?!(forms/|forms$)).*)?$" />
              <action type="Redirect" url="http://{HTTP_HOST}{R:1}" redirectType="Permanent" />
          </rule>
      

      编辑:我已将模式移至标签本身,因为将所有内容与 .* 匹配,然后使用附加条件似乎没有必要。我还更改了重定向 URL 以使用匹配中括号捕获的输入 URL 部分。

      【讨论】:

      • 感谢 sundar,它似乎仍然不起作用,我认为它非常接近,只是不确定为什么它不会获取我的域的根 (domain.com) 并将其重定向到http 等价物
      • @Burt 你的意思是它适用于除根 domain.com 以外的所有页面吗?如果是这样,我实际上会感到惊讶,因为如果我理解正确,之前重定向 URL 中的 {R:0} 应该会搞砸。我已对帖子进行了一些更改,请尝试此版本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多