简体   繁体   English

如何在.htaccess重定向中使用两个变量?

[英]How do I get two variables to use in an .htaccess redirect?

I'm trying to redirect based on a cookie value AND a query string value but can't seem to get both values to return properly. 我正在尝试根据Cookie值和查询字符串值进行重定向,但似乎无法使两个值都能正确返回。 I'm using: 我正在使用:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_COOKIE} secret=([^;]+) [AND]
RewriteCond %{QUERY_STRING} ^file=(\w+)$
RewriteRule ^downloads/(.*)/$ /downloads/%1/%2

and using the URL "/downloads/?file=protected/doc.pdf". 并使用网址“ /downloads/?file=protected/doc.pdf”。 I'd like the redirect to go to "/downloads/[secret cookie_val]/protected/doc.pdf" 我想重定向到“ / downloads / [secret cookie_val] /protected/doc.pdf”

Thanks! 谢谢!

There is no [AND] flag. 没有[AND]标志。 Conditions are by default "anded" together. 默认情况下,条件一起“与”。

Also, if you are requesting /downloads/?file=protected/doc.pdf , you've got one too many / 's in your regex: 另外,如果您请求/downloads/?file=protected/doc.pdf ,则正则表达式中的/太多了:

RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING}:::%{HTTP_COOKIE} ^file=(\w+):::secret=([^;]+)
RewriteRule ^downloads/(.*)/?$ /downloads/%2/%1

EDIT: Just realized that you were trying to access a previous RewriteCond back reference using %2 , which is going to be blank because you've created another grouping with your %{QUERY_STRING} . 编辑:刚意识到您正在尝试使用%2访问先前的RewriteCond反向引用,该引用将为空白,因为您已经用%{QUERY_STRING}创建了另一个分组。 So you need to combine the 2 vars into one RewriteCond and match against: `%{QUERY_STRING}:::%{HTTP_COOKIE}. 因此,您需要将2个变量组合为一个RewriteCond并与以下对象进行匹配:%{QUERY_STRING} :::%{HTTP_COOKIE}。 Then you'll be able to backreference both the query string and cookie. 然后,您将能够同时反向引用查询字符串和cookie。

And the RewriteCond %{ENV:REDIRECT_STATUS} !200 is to prevent rewrite engine looping. RewriteCond %{ENV:REDIRECT_STATUS} !200用于防止重写引擎循环。

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

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