简体   繁体   English

IIS重写规则未设置HTTP_COOKIE

[英]IIS Rewrite Rule is not setting HTTP_COOKIE

I'm working with a client who has a mobile site setup through a third party. 我正在与通过第三方设置移动网站的客户合作。 Currently, we check through IIS whether a user agent matches any of your standard mobile agents in which case we redirect the user to the mobile version m.whatever.com. 目前,我们通过IIS检查用户代理是否与您的任何标准移动代理匹配,在这种情况下,我们将用户重定向到移动版m.whatever.com。

One of the rules we have requires us to set the cookie to a value of 0 when the user wants to see the mobile site again. 我们的规则之一要求我们在用户想要再次查看移动网站时将cookie设置为值0。

<rules>
    <rule name="if httpcookie is , set it to 0" stopProcessing="true">
        <match url="^(.*)$" />
        <conditions>
            <add input="{HTTP_COOKIE}" pattern="mobileoptout=1" />
        </conditions>
        <serverVariables>
            <set name="HTTP_COOKIE" value="mobileoptout=0" />
        </serverVariables>
        <action type="None" />
    </rule>
</rules>

As per the above, we're matching on the URL and the cookie value. 根据以上所述,我们匹配URL和cookie值。 I have tested these independently and they work as expected. 我已经独立测试了它们,它们按预期工作。 However, at the end of this rule, the value for the cookie MobileOptOut is still 1 and not 0. 但是,在此规则结束时,cookie MobileOptOut的值仍为1而不是0。

I've searched and tried all the examples available on numerous sites but have been completely unable to understand why the value of the cookie is not being changed. 我已经搜索并尝试了许多网站上提供的所有示例,但完全无法理解为什么cookie的值没有被更改。

The domain for the cookie is [whatever.com] which is the same as www.whatever.com and based on previous tests it can read from the cookie to validate a condition. cookie的域名是[whatever.com],它与www.whatever.com相同,并且基于先前的测试,它可以从cookie中读取以验证条件。

Any ideas? 有任何想法吗?

Including an additional attempt which does not work either: 包括一项不起作用的额外尝试:

<rules>
   <rule name="set cookie">
     <match url="(.*)" />
     <serverVariables>
       <set name="HTTP_COOKIE" value="optout=1" />
       <set name="{HTTP_COOKIE}" value="optout=2" />
     </serverVariables>
     <action type="None" />
   </rule>
</rules>

Thanks to @cheesemacfly I looked into using the outbound rules, which for some reason I completely overlooked. 感谢@cheesemacfly,我研究了使用出站规则,出于某种原因我完全忽略了这些规则。

The solution uses a single inbound rule to check two conditions. 该解决方案使用单个入站规则来检查两个条件。

<rule name="MobileOptOut=1 Stop Processing Rules" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
    <add input="{QUERY_STRING}" pattern="mobileoptout=1" />
    <add input="{HTTP_COOKIE}" pattern="MobileOptOut=1" />
  </conditions>
  <action type="None" />
</rule>

This will handle the first request with the querystring and all subsequent requests with the cookie that will be created after this rule finishes. 这将处理带有查询字符串的第一个请求以及将在此规则完成后创建的cookie的所有后续请求。

The outbound rule is as follows: 出站规则如下:

<outboundRules>
  <rule name="if querystring=1" preCondition="If mobileoptout query = 1">
    <match serverVariable="RESPONSE_Set_Cookie" pattern="." />
    <action type="Rewrite" value="mobileoptout=1; Domain=site.local; Path=/;" />
  </rule>
  <preConditions>
    <preCondition name="If mobileoptout query = 1">
      <add input="{QUERY_STRING}" pattern="mobileoptout=1" />
    </preCondition>
  </preConditions>
</outboundRules>

This will check that first condition of if querystring is mobileoptout=1. 这将检查if querystring的第一个条件是mobileoptout = 1。 If true, it will set a cookie in the response named 'mobileoptout' with a value of 1 in the root domain that will expire with the session. 如果为true,它将在名为“mobileoptout”的响应中设置一个cookie,其根域中的值将为1,该会话将随会话一起到期。

This is exactly what I was overlooking. 这正是我所忽视的。

First, to make sure your rule can work, you have to setup the allowed server variables. 首先,要确保您的规则可以正常工作,您必须设置允许的服务器变量。

To do so, in the iis manager, under the URL Rewrite section, click on View Server Variables... : 为此,在iis管理器的URL Rewrite部分下,单击View Server Variables...

菜单

You then can add a new variable by clicking Add... on the right. 然后,您可以通过单击右侧的Add...添加新变量。
In your case, you want to add HTTP_COOKIE : 在您的情况下,您想要添加HTTP_COOKIE

HTTP_COOKIE

From here, your rule (as following) should be working: 从这里开始,你的规则(如下)应该有效:

<rules>
    <rule name="if httpcookie is , set it to 0" stopProcessing="true">
        <match url="^(.*)$" />
        <conditions>
            <add input="{HTTP_COOKIE}" pattern="mobileoptout=1" />
        </conditions>
        <serverVariables>
            <set name="HTTP_COOKIE" value="mobileoptout=0" />
        </serverVariables>
        <action type="None" />
    </rule>
</rules>

Note that the cookie is rewritten only for the request. 请注意,cookie仅针对请求进行重写。
Meaning the client cookies won't be changed but the request hitting the page will have the new cookie values when the rule is triggered. 这意味着客户端cookie不会被更改,但是当触发规则时,命中页面的请求将具有新的cookie值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM