random_user_name
random_user_name

Reputation: 26160

How To Allow Access to Certain File Types but Deny All Others

While I'm confident this has been asked and answered somewhere, my Google and SO searches have not helped me solve what seems like a fairly easy problem.

The goal:
Deny access to ALL file types except images.

Current .htaccess file:

<Files *.*>
  Order Deny,Allow
  Deny from all
</Files> 
<FilesMatch "\.(jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG)$">
  Order Deny,Allow
  Allow from all
</FilesMatch> 

I still cannot (via the browser) access any image files, with a "403 Forbidden" error.

Questions:
1. How do I make this work properly without rewrite rules?
2. Can I combine Files and FilesMatch rules like this?
3. Are the FilesMatch rules case sensitive?

Upvotes: 1

Views: 2243

Answers (1)

anubhava
anubhava

Reputation: 785098

You can easily achieve this via mod_rewrite rules:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule !\.(jpe?g|png|gif)$ - [NC,F]

Using FilesMatch you could do this:

Order deny,allow

# first deny all files
<Files *>
deny from all
</Files>

# then allow all image files
<FilesMatch "(?i)\.(jpe?g|png|gif)$">
  allow from all
</FilesMatch>

Upvotes: 2

Related Questions