8

我需要一个匹配除特定路径之外的所有 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>

但它不起作用

4

4 回答 4

5

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

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

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

于 2013-08-05T20:53:16.687 回答
5

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

<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/or开头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>
于 2013-08-06T14:24:48.997 回答
4

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

解释:

  • ^: 匹配字符串的开头
  • https://: 匹配https://
  • [^/]+:匹配除正斜杠之外的任何内容一次或多次
  • (: 开始匹配组 1
    • /: 匹配/
    • (?!: 负前瞻
      • form/: 检查是否没有form/
      • |: 或者
      • form$form: 检查字符串末尾是否没有
    • ): 结束负前瞻
    • .*: 匹配所有内容零次或多次
  • ): 结束匹配组 1
  • ?: 使前一个令牌可选
  • $: 匹配行尾
于 2013-08-05T20:53:08.337 回答
3

我在发布的模式中看到两个问题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 部分。

于 2013-08-05T21:23:21.297 回答