【问题标题】:What is the meaning of pattern="." on preCondition property of IIS Url Rewrite?pattern="." 是什么意思在 IIS Url Rewrite 的 preCondition 属性上?
【发布时间】:2026-01-06 16:05:02
【问题描述】:

我见过很多 IIS URL Rewrite 的例子:

<!-- reference: https://im5tu.io/article/2017/06/ensuring-samesite-cookies-with-url-rewrite/ -->
<rewrite>
  <outboundRules> 
    <rule name="Ensure samesite Cookies" preCondition="Missing samesite cookie">
      <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
      <action type="Rewrite" value="{R:0}; SameSite=strict" />
    </rule>
    <preConditions>
      <preCondition name="Missing samesite cookie">
        <!-- Dont remove the first line here, it does do stuff! -->
        <add input="{RESPONSE_Set_Cookie}" pattern="." />
        <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=strict" negate="true" />
      </preCondition>
    </preconditions>
  </outboundRules>
</rewrite>

有人知道为什么&lt;add input="{RESPONSE_Set_Cookie}" pattern="." /&gt; 这行是必要的吗?

我尝试在 Google 上搜索,但没有找到答案。

【问题讨论】:

    标签: asp.net asp.net-mvc iis


    【解决方案1】:

    该规则仅在与前置条件匹配时才执行。

    “模式”是正则表达式,. 匹配任何字符。
    在这种情况下,基本上是说“如果 cookie 有值则匹配”。

    如果 cookie 已经具有要添加的值,则它下面的行否定执行规则。
    否则每次运行规则时,您都会再次添加文本。

    至于为什么需要,发送一个空cookie应该删除它。
    在这种情况下,它将确保 cookie 中确实存在一个值,然后再尝试使用规则对其进行更改。

    Rewrite Module - Preconditions Collection
    Creating outbound rules for URL rewrite module

    【讨论】:

      【解决方案2】:

      这是一个正则表达式。

      在正则表达式中,. 匹配任何单个字符。但仅凭这一点并不能很好地解释事情。要了解的重要一点是空字符串将 NOT 匹配,因为没有字符。其他任何东西——任何东西,不管多长时间——匹配,因为一旦与第一个字符确认匹配,处理就会停止。

      换句话说,这个表达式检查cookie是否真的有值。

      【讨论】:

        最近更新 更多