我尝试获取请求参数“授权”的值,并将其存储在请求的标题“授权”中。
第一个重写规则工作正常。 在第二个重写规则中,$ 2的值似乎并不存储在环境variables中。 结果请求头“授权”是空的。
任何想法 ? 谢谢。
<VirtualHost *:8010> RewriteLog "/var/apache2/logs/rewrite.log" RewriteLogLevel 9 RewriteEngine On RewriteRule ^/(.*)&authorization=@(.*)@(.*) http://<ip>:<port>/$1&authorization=@$2@$3 [L,P] RewriteRule ^/(.*)&authorization=@(.*)@(.*) - [E=AUTHORIZATION:$2,NE] RequestHeader add "Authorization" "%{AUTHORIZATION}e" </VirtualHost>
我需要处理几个案例,因为有时参数在path中,而他们在查询中。 取决于用户。 这最后一个案件失败。 AUTHORIZATION的标题值看起来是空的。
# if the query string includes the authorization parameter RewriteCond %{QUERY_STRING} ^(.*)authorization=@(.*)@(.*)$ # keep the value of the parameter in the AUTHORIZATION variable and redirect RewriteRule ^/(.*) http://<ip>:<port>/ [E=AUTHORIZATION:%2,NE,L,P] # add the value of AUTHORIZATION in the header RequestHeader add "Authorization" "%{AUTHORIZATION}e"
看起来你在第一条规则上有'L'(last)标志。 规则处理将停在那里,不会再发生重写。 我不认为会达成第二条规定。 尝试删除“L”标志。
编辑
哦,你也有'P'(代理)标志。 这也将停止重写规则处理,并通过mod_proxy强制代理请求。
你能用一条规则来做这一切,因为模式匹配是一样的。 我不完全确定你在做什么,但这可能会做到这一点:
RewriteRule ^/(.*)&authorization=@(.*)@(.*) http://<ip>:<port>/$1&authorization=@$2@$3 [E=AUTHORIZATION:$2,NE,L,P]
更新
啊哈,我想我明白你现在要做的是什么。 只要您在标志中指定了[P],代理请求就会发生在这一点上。 如果我正确地阅读了这个问题,你希望把AUTHORIZATION var传递给那个请求,所以你需要把它放在[P]之前:
# if the query string includes the authorization parameter RewriteCond %{QUERY_STRING} ^(.*)authorization=@(.*)@(.*)$ RewriteRule ^/(.*) - [E=AUTHORIZATION:%2] # add the value of AUTHORIZATION in the header RequestHeader add "Authorization" "%{AUTHORIZATION}e" # keep the value of the parameter in the AUTHORIZATION variable and redirect RewriteRule ^/(.*) http://<ip>:<port>/ [NE,L,P]
完全未经testing,但应该做你想做的 – 如果我正确地理解了这个问题。
Apacheconfiguration更新
你在httpd.conf中为那个目录设置了AllowOverride FileInfo吗? 如果没有,那么你将无法在.htaccess中使用RequestHeader
我怀疑你不能覆盖Authorization
标题,或者在请求过程中稍后被修改。 正如我相信你知道的, Authorization:
用于HTTP基本authentication,所以很有可能是别的东西在干扰它。 你可以使用不同名称的头文件吗?
有没有一个原因,你使用mod_rewrite
而不是mod_setenvif
?
http://httpd.apache.org/docs/current/mod/mod_setenvif.html#setenvif