简体   繁体   中英

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. 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". I'd like the redirect to go to "/downloads/[secret cookie_val]/protected/doc.pdf"

Thanks!

There is no [AND] flag. 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:

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} . So you need to combine the 2 vars into one RewriteCond and match against: `%{QUERY_STRING}:::%{HTTP_COOKIE}. Then you'll be able to backreference both the query string and cookie.

And the RewriteCond %{ENV:REDIRECT_STATUS} !200 is to prevent rewrite engine looping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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