I am working on a little framework where I have this directory tree:
project/
|--- ... (directories unrelated to the question)
|--- public/
| |--- index.php
.htaccess
The .htaccess sends every request to project/public/index.php but I have to add specific rules for assets (stylesheets, js, images, ...).
Searching a lot I started adding a RewriteCond for specific extensions as I saw almost everywhere, and RewriteRule to catch parts of the URL starting with folder names like css/ etc:
# Send front-end assets in the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \.(bmp|css|gif|ico|jpe?g|js|png|tiff)$
RewriteRule ((css|js|img)\/.*\..*)$ public/$1 [L,QSA]
But this means I would define which extensions are allowed. And this would also mean I would define a specific list of subdirectories allowed in public/.
I'm not an expert with mod_rewrite, but I just thought I could define an "assets prefix" which, if present in URL, would rewrite the rest of the URL to the public directory.
Example URL:
| detect | rewrite to /public/$1
some_route_to_strip/_prefix_/any_custom_dir/any_file.ext
- Should I avoid doing that ? If so, why ?
- If not, is the way to handle requests to asset files I almost always encountered a good way to do ?
/a/b/c/foo.cssand then rewriting it to/public/foo.csswhy not tell the browser to fetch/public/foo.css?/_front/foo.csswhich would rewrite to/public/foo.cssto avoid rewriting to the Front Controller like/public/index.php?_url=/public/foo.css. But I think I was wrong about my .htaccess configuration at the time I wrote that, this can be served directly, only the application should write the correct path to the assets.