I have a website that has a folder for images.
I have two problems:
- I want to disable all script execution in that directory (i.e. no PHP/Perl/Python
anything.) - There are two php files in my images folder called gradient.php and rgba.php – I do what those to run as per usual.
How do I set up my .htaccess file to do that. Also, rather than placing a new .htaccess in the images directory, is it possible to incorporate these directives in the one in the site root?
You can add these rules to the htaccess file in your site root:
This should make it so any request for
/images/that doesn’t end with jpg/jpeg/png/gif/bmp (or whatever other extension you want to add to the regular expression) or isn’tgradient.phporrgba.php, will result in a 403 forbidden.EDIT:
as long as jpg and other images are mapped to the correct mime/type (via
AddType image/jpeg .jpg) then it won’t get handed to the php handler and whatever code is there won’t get executed. If you want to serve all files using the default handler, you need to setAddHandler default-handler phpin the htaccess file in your images directory. You’ll then need to move the gradient and rgba files out to some other directory. You can’t selectively set handlers from an htaccess file, though you may be able to use<Location>blocks to set handlers in your vhost config.EDIT 2:
I was wrong, you can use the
Hflag to set a custom handler using a rule. So in the above rules, instead of[L,F], you can do[L,H=default-handler]so that anything that isn’t an image orgradient.phporrgba.phpwill get sent to the default-handler (e.g. php files will get sent as-is, and not handled and executed by mod_php).So you can just do: