Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mod_Rewrite to check multiple folders for a static file

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.

like image 675
DavidWinterbottom Avatar asked Feb 01 '10 12:02

DavidWinterbottom


2 Answers

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}/library2%{REQUEST_URI} -f
RewriteRule ^css/.+ library2%{REQUEST_URI} [L]
like image 62
Gumbo Avatar answered Nov 14 '22 22:11

Gumbo


RewriteMap Directive might help.

like image 20
ziya Avatar answered Nov 14 '22 23:11

ziya