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.

Accepted Answer

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]
Written by Gumbo
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki