Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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.