.htaccess to only allow images to display in directory


I’m having problems getting this .htaccess file to work. I would like to put the following in the parent level directory of the images directory so that only images can be displayed and nothing else. What’s happening is that none of the images in the directories will display. However, I only want the specified images to display and no other file format.

Here’s the code:

deny from all

<Files ~ "^\\w+\\.(gif|jpe?g|png)$">
order deny,allow
allow from all

Do you see anything wrong with the above code?




Yes, I don’t believe that <FILES> can accept a NOT modifier. It’s easier to use mod_rewrite (if available) to ONLY serve your image files:

# .htaccess in your images directory
RewriteEngine on
RewriteCond &#37;{REQUEST_URI} ^\\.(gif|jpe?g|png)$ [NC]
RewriteRule .? - [F,L]

That checks for the image extensions and, if not found, will send a FAIL code to the browser (for that request).



Thank you. I will try that.

Could I set this in the web root’s .htaccess file and then set a rewrite rule for each directory that I would like to only display images?

Something like this?

limit upload images directory for members

RewriteEngine on
RewriteCond %{REQUEST_URI} ^upload\/members\/.*\.(gif|jpe?g|png)$ [NC]
RewriteRule .? - [F,L]

limit upload images directory for groups

RewriteEngine on
RewriteCond %{REQUEST_URI} ^upload\/groups\/.*\.(gif|jpe?g|png)$ [NC]
RewriteRule .? - [F,L]


Yes, but why two RewriteEngine on statements and the unnecessary escapting of /'s?