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

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

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

and I want the queries to /css/somefile.cssfirst check the directory public/css/, then cascade to public/library1/css/, then public/library2/css/return 404 if the object cannot be found in any of the directories.

I thought line by line:

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 does not work - I'm not sure how to check for the existence of a file on a dynamically generated path.

+5
source share
4 answers

Try the following 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]
+6
source
+1

Perhaps the server variables do not contain what you think. Try increasing your logging for debugging so you can see exactly what is going on.

+1
source

Try the following:

# Loads files from production server
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (library1|library2)/(.*)$ http://production.com/$1/$2 [R=302,L,NC]

To achieve this:

something.dev/ library1 /style.css → production.com/ library1 /style.css something.dev/ library2 /vendor/css/style.css → production.com/ library2 /vendor/css/style.css

0
source

All Articles