I am trying to use the result of a RewriteMap function to conditionally allow access to a directory.
The intention is to read a timestamp from a cookie (my_cookie) and pass it into the RewriteMap (my_rewrite_map_func) which I have defined in httpd.conf (It's an executable which echos strings "TRUE" or "FALSE" to stdout if the timestamp in the cookie is within a certain range of the current time in Apache).
RewriteMap my_rewrite_map_func prg:/var/www/program
The contents of my .htaccess file are:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} my_cookie=([^;]+) [NC]
RewriteCond ${my_rewrite_map_func:%{TIME}%1|FALSE},FALSE ^([^,]+),\1 [NC]
RewriteRule ^(.*)$ / [NC,L,QSA,F]
I can confirm that the program itself is working, the cookie is being read, and the Apache timestamp and the cookie timestamp are within the allowed range.
The regex on the second RewriteCond checks if the return value of ${my_rewrite_map_func:%{TIME}%1|FALSE} is FALSE, however, regardless of what I set this to, the RewriteRule never occurs.
Essentially, I cannot determine how to evaluate the value of ${my_rewrite_map_func:%{TIME}%1|FALSE}. Is there a way I can better extract or store the value of this?
Any help would be most appreciated.
UPDATE - SOLUTION: I'm not sure why this question was flagged as off topic, it relates to protecting html files from a php script by means of an Apache .htaccess file using mod_rewrite to call a C++ program.
Regardless, the above code works perfectly when used in conjunction with two additional lines to handle the case of the cookie not existing:
RewriteCond %{HTTP_COOKIE} !^.*my_cookie.*$ [NC]
RewriteRule ^(.*)$ / [NC,L,QSA,F]
Hopefully this will help others who have experienced difficulty with the same issue; it seems there has been little success with this elsewhere on the web.
My specific case deals with preventing direct access to html and other files without modifying them directly, or using a download script. PHP code is used to generate a page with links to these files, from which Javascript performs an ajax call to retrieve the server's timestamp and sets a cookie. The timestamp in the cookie is compared to the time in Apache at page load, if it's within a certain range access is granted.
Here's how I do (you may think it's a lot of instructions, but it's sooo quick compared to PHP handling that you should not worry about 5 (or something) cond instead of 2 (or something)):
RewriteEngine On
RewriteCond %{HTTP_COOKIE} my_cookie=([^;]+) [NC]
# Don't touch anything but create MYCOOKIE environment
# and set it to empty if not found:
RewriteRule . - [E=MYCOOKIE:${my_rewrite_map_func:%{TIME}%1|}]
# If the environment was found = not empty:
RewriteCond %{E:MYCOOKIE} !^$ [NC]
# ... then process a rewrite rule:
RewriteRule [blabla...blabla]
I do some stuff like that 50 times and my web server is still amazingly fast. (the bottleneck is the database)
Hope this helps
来源:https://stackoverflow.com/questions/8453554/apache-rewritemap-used-to-prevent-direct-access-to-files