views:

26

answers:

3

What are the mod_Rewrite rules for checking multiple folder locations for a given file. For instance, if I have a folder structure of:

public/
    css/
    library1/
        css/
    library2/
        css/

and I want requests to /css/somefile.css to first check the public/css/ directory, then cascade to public/library1/css/ then public/library2/css/, returning a 404 if an asset cannot be found in any of the directories.

I was thinking along the lines of:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond library1%{REQUEST_URI} -f
RewriteRule ^(.*)$ library1$1 [L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond library2%{REQUEST_URI} -f
RewriteRule ^(.*)$ library2$1 [L]

But this doesn't seem to work - I'm not sure how to check for file existence on a dynamically generated path.

A: 

RewriteMap Directive might help.

Maxwell Troy Milton King
A: 

Possibly the server variables do not contain what you think. Try increase the logging to debug, so you can see exactly what is going on.

Dan Andreatta
+1  A: 

Try these rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/library1%{REQUEST_URI} -f
RewriteRule ^css/.+ library1%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/library1%{REQUEST_URI} -f
RewriteRule ^css/.+ library2%{REQUEST_URI} [L]
Gumbo
Cheers for that - I didn't spot the %{DOCUMENT_ROOT} variable when reading the docs.
DavidWinterbottom
@DavidWinterbottom: You need the absolute filesystem path when you want to test for existing files, directories etc. *REQUEST\_FILENAME* does already contain the absolute filesystem path but *REQUEST\_URI* only the absolute URL path.
Gumbo