简体   繁体   English

如何用mod_rewrite更改cookie名称?

[英]How to change a cookie name with mod_rewrite?

I'm trying to change the name of a cookie that's set by an AWS ELB, but keep its value with a rewrite condition and rewrite rule. 我正在尝试更改由AWS ELB设置的cookie的名称,但是将其值保留为重写条件和重写规则。

Here's what I've managed so far: 这是我到目前为止所管理的内容:

RewriteCond %{HTTP_COOKIE} AWSELB=(^BD.*) [NC]
RewriteRule ^(.*) - [CO=SIMELB:%1:.amazonaws.com:lifetime:-1]

Obviously the RewriteRule is incorrect, but could someone help me with the right syntax? 显然RewriteRule是不正确的,但有人可以用正确的语法帮助我吗?

Ok, following the comment thread, I think there's enough info to get started. 好的,在评论主题之后,我认为有足够的信息可以开始。 Foremost, your pattern doesn't work because of the (^BD.*) capture group, and in particular because of the ^ anchor. 最重要的是,由于(^BD.*)捕获组,您的模式不起作用,特别是因为^锚。 Instead, capture (BD[^;]+) to grab everything up to the next ; 相反,捕获(BD[^;]+)以抓住所有内容到下一个; (or the end of the string if there isn't one). (如果没有字符串,则为字符串的结尾)。

To explicitly unset the previous cookie, other examples use the INVALID modifier, though I cannot find the documentation for it. 要显式取消设置上一个cookie,其他示例使用INVALID修饰符,但我找不到它的文档。

Apache mod_rewrite documentation on Cookies 关于Cookies的Apache mod_rewrite文档

RewriteCond %{HTTP_COOKIE} AWSELB=(BD[^&]+) [NC]
# Delete the old one
RewriteRule ^ - [CO=AWSELM:INVALID:.amazonaws.com:0:/:-1]
# Add the new one
# Specify your lifetime in minutes or 0 for the browser session (60 below)...
# ALso add the path
# Assumimg the -1 is for insecure cookies
RewriteRule ^ - [CO=SIMELB:%1:.amazonaws.com:60:/:-1]

For the old cookie to be successfully unset, both the domain and the path will need to exactly match those originally set by AWS. 要成功取消设置旧cookie,域和路径都需要与AWS最初设置的完全匹配。 Inspect the cookies currently being set and make sure you match the domain & path. 检查当前正在设置的cookie,并确保您匹配域和路径。

And really, it isn't necessary to match BD ... You could just as well do AWSELB=([^;]+) because it must only match up to the following semicolon anyway. 实际上,没有必要匹配BD ......你也可以做AWSELB=([^;]+)因为它必须只匹配以下分号。

Addendum: 附录:

If the value is being lost, it may be because the the RewriteCond is only applied to the first subsequent matching RewriteRule . 如果该值丢失,可能是因为RewriteCond仅应用于第一个后续匹配的RewriteRule You can always just repeat the RewriteCond . 你总是可以重复RewriteCond This is ugly, unfortunately, but I tested it and found it to work correctly. 遗憾的是,这很难看,但我测试了它并发现它能够正常工作。

# no capture group the first time since you don't use it until later
RewriteCond %{HTTP_COOKIE} AWSELB=BD.+ [NC]
RewriteRule ^ - [CO=AWSELM:INVALID:.amazonaws.com:0:/:-1]
# This will continue to execute since the previous didn't have [L]
RewriteCond %{HTTP_COOKIE} AWSELB=(BD[^&]+) [NC]
RewriteRule ^ - [CO=SIMELB:%1:.amazonaws.com:60:/:-1]

(Note: you won't see the cookie value updated until a subsequent HTTP request; that is, if you tried to inspect it from your script right after setting it with Apache, the new value won't be present because the cookie header has to make a round trip back to the client) (注意:在后续HTTP请求之前,您不会看到cookie值更新;也就是说,如果您在使用Apache设置后立即从脚本中检查它,则新值将不会出现,因为cookie标头具有往返客户回程)

Instead of trying to rewrite the cookie name, I tested with mod_header directives and seem to have addressed my issue with Amazon's ELB cookie breaking session affinity with another Amazon ELB. 我没有尝试重写cookie名称,而是使用mod_header指令进行了测试,似乎已经解决了我的问题,亚马逊的ELB cookie与其他Amazon ELB打破了会话关联。

RequestHeader edit Cookie AWSELB SIMELB
RequestHeader edit Cookie APPELB AWSELB
Header always edit Set-Cookie AWSELB APPELB
Header edit Set-Cookie AWSELB APPELB

This so far seems to work, relying on the browser to maintain the memory for me because after the retrieving the value of the first AWSELB on request, when I get the set-Cookie response back from the second AWSELB, the browser sees APPELB={value} and recalls the correct request cookie obtained from the first AWSELB. 到目前为止,这似乎是有效的,依赖于浏览器为我维护内存,因为在请求检索第一个AWSELB的值后,当我从第二个AWSELB获得set-Cookie响应时,浏览器看到APPELB = { value}并回忆从第一个AWSELB获得的正确请求cookie。

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

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