I need a rule in Apache that redirects not-found CSS files to another location based on their names in another folder. Like this:
- Request:
localhost/css/nonexistent.css - Response:
localhost/css/g/nonexistent.css
If the CSS exists, just serve it like normal:
- Request:
localhost/css/existent.css - Response:
localhost/css/existent.css
My project is on CakePHP which comes with the following rules by default:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
I mention it because whatever the new rule is, it should not break Cake’s rules.
Thanks for the help.
Edit: Forgot to mention that the css/g/ file is an alias for a script (inside the Cake MVC stack) that generates the new css file and echoes it. Answers so far seem to do the redirection fine, but then it doesn’t find the css/g/file.css either because it really doesn’t exist.
You can try first checking if the /css/g/ css file exists:
The %1 in the 3rd condition backreferences the filename (sans .css extension) matched in the previous
RewriteCond.EDIT:
If the file css file is actually generated, then skip the checking of /g/ version and just pass it to the controller: