1

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 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.