正则表达式匹配除特定路径之外的所有https URL

Bur*_*urt 7 regex iis url-rewriting

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

例如

比赛

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>
Run Code Online (Sandbox Code Playgroud)

但它不起作用

Ham*_*mZa 5

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

解释:

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


Bla*_*ppo 5

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

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

在后www.domain.com位,它在寻找字符串的任何一个结束,或为一斜线,然后东西是不是forms


che*_*fly 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>
Run Code Online (Sandbox Code Playgroud)

仅当请求为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>
Run Code Online (Sandbox Code Playgroud)