IIS web.config 重写规则条件输入内部两个捕获组的比较

Comparison between two capture groups inside condition input of IIS web.config rewrite rule

我正在尝试比较 IIS web.config 中重写 url 的条件输入中的两个捕获组。

我需要这样做,因为我不希望用户在他们的语言代码 cookie 与 URL。

我试图在我的 webconfig 中使用此规则来实现,但似乎无法这样做,因为服务器返回 500.52 错误:

The expression "^((?!{C:2}).)*$" contains a repeat expression (one of '*', '?', '+', '{' in most contexts) that is not preceded by an expression.

它将我的 {C:2} 作为正则表达式本身的一部分,而不是原始字符串。

这是完整的规则:

<rule name="redirect-with-lang" stopProcessing="true">
      <match url="en\/|es\/|mx\/" ignoreCase="true"/>
      <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
        <add input="{HTTP_COOKIE}" pattern="langpref=\w{2}\/(\w{2});" />
        <add input="{REQUEST_URI}" pattern="^(\/es\/|\/en\/|\/mx\/).+$" />
        <add input="{C:1}" pattern="^((?!{C:2}).)*$"/>
      </conditions>
      <action type="Redirect" url="/{C:1}/{C:3}" />
    </rule>

我想知道是否可以通过另一种方式实现此目的而无需借助 javascript 或 Global.asax 上的某些服务器代码。

谢谢

您可以使用此 <add input="{C:1}" pattern="^((?!\{C:2\}).)*$"/> 条件而不是 <add input="{C:1}" pattern="^((?!{C:2}).)*$"/>

您收到此错误是因为条件中的大括号 – { }。