如果 cookie 丢失,您如何使用 ARR 重写 url?

Using ARR how do you rewrite a url if a cookie is missing?

我知道我可以使用规则的条件部分中的 {HTTP_COOKIE} 变量,根据来自 cookie 的值重写 url。此规则获取一个名为 ServerProxy 的 cookie 并重写该服务器 url.

<rule name="SendTrafficToServerProxyCookieValue" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="http://{C:1}/{R:0}" />
    <conditions>
        <add input="{HTTP_COOKIE}" pattern="ServerProxy=(.*)" />
    </conditions>
</rule>

如果 ServerProxy cookie 不存在或未设置,我想将流量定向到名为 authenticate.app 的身份验证服务器。我该如何编写重写规则来做到这一点?

试试这个:

<rule name="SendTrafficToServerProxyCookieValue" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="http://{C:1}/{R:0}" />
    <conditions>
        <add input="{HTTP_COOKIE}" pattern="ServerProxy=(.+)" />
    </conditions>
</rule>
<rule name="DoAuthRewrite" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="SOMETHING_ELSE" />
    <conditions>
        <add input="{HTTP_COOKIE}" pattern="ServerProxy=(.+)" negate="true" />
    </conditions>
</rule>

请注意,* 已更改为 + 以确保 cookie 不为空。取反只是翻转条件,使其为空或不存在。