Httpd – Setting permissions for mp4 and png files in .htaccess

.htaccessapache-2.2httpdpermissions

First, I'm brand new to Apache .htaccess. I've got a server that must allow users to dive only into mp4 and png files. So, I've been reading Apache HTTP Server documentation and, according to what it says, I've added this to my .htaccess:

<filesMatch "\.(png|mp4)$">
Order Allow,Deny
Allow from all
</filesMatch>

because, as far as I've understood, this means "for every file, if its extension is png or mp4, allow everyone to grab it. Otherwise deny access". Obviously, I'm wrong, because this forbids me from accessing any files/folders allows me to access folders and html files (as well as of course png and mp4 files) anyway. So, how would it be to allow access only to png and mp4 files? As well, I'd like to know how to make the filter to ignore case.

Thanks.

EDIT: First – Forgot to say I need to allow localhost to access everything (i.e., the denials must be only for non-localhost). I don't really know if this is the default behaviour or is it necessary to indicate it somehow. As well, I had a corrupt file when I did the tests so that's why I was always getting 'forbidden access' to everything. I've updated the post.

Best Answer

Try

Order Allow,Deny
<filesMatch "\.(?i:png|mp4)$">  
    Allow from all
</filesMatch>

The ?i: part should allow for case insensitive matching, and putting the order outside the FilesMatch make it deny everything not explicitly allowed.