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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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