Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I match against the URL-path from the RewriteCond in a .htaccess?

I'd like this because I'm trying make one subdirectory of my web server transparently proxy + cache requests to another server, and I'd like to write the .htaccess like this:

RewriteCond "%{ENV:CACHE_BASE}/%{URL_PATH}" -f
RewriteRule (.*) "%{ENV:CACHE_BASE}/$1" [L]
RewriteRule (.*) "download_and_cache.cgi?$1"

So that requests for files which already exist in the cache will be sent from within Apache, and requests for files which aren't in the cache will be handled by the download_and_cache.cgi script (which will put them in the cache).

NB: I can't use %(REQUEST_URI}, because if the .htaccess lives at /foo/, a request for /foo/README.txt will result in a cache check for $CACHE_BASE/foo/README.txt, instead of $CACHE_BASE/README.txt as it should.

share|improve this question

1 Answer 1

My Mod_rewrite is a little rusty but shouldn't you be able to use the RewriteBase directive to account for "/foo"

Or I think you could use a back reference something like this

RewriteCond %{REQUEST_URI} foo/(.*)
RewriteCond "%{ENV:CACHE_BASE}/%1" -f

Or if I'm following it all correctly Rewrite first to the cache, then check to see if you need to download it first. Like so:

RewriteRule foo/(.*) "%{ENV:CACHE_BASE}/$1" [L]
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule (.*) "download_and_cache.cgi?$1"

If you can using FallbackResource would make the above a little cleaner.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.