How can I disallow everything in a directory except any subdirectories that have a certain name using htaccess or even vhost.conf for the site? I have a protected main directory which contains subdirectories. Each of the subdirectories have another child dir named 'thumbs', that I want to allow people access to. In the thumbs folder I have files which all start with 'thumb_' prefix for the filename. So which approach to use, .htaccess FilesMatch or vhost DirectoryMatch? and what would be the code to use? Thanks
1 Answer
So if I'm understanding this right, you'll only want to allow files named thumb_*
if they're inside a directory named thumbs
, which is inside a subdirectory of the protected directory? It'd be simpler to not enforce the directory being named thumbs
, since then you'd just need the <FilesMatch>
.
Sounds like the path is, for example, /path/to/protected_dir/subdir/thumbs/thumb_1.png
If that's right, this should enforce the kinds of restrictions that you're looking for - just make sure there aren't other config blocks that might interfere with the permission settings:
<Directory /path/to/protected_dir>
Order Deny,Allow
Deny from all
</Directory>
<LocationMatch "^/url/path/to/protected_dir/[^/]+/thumbs/thumb_.*">
Allow from all
</LocationMatch>
If you're not worried about enforcing the exact directory tree structure, this is a lot simpler:
<Directory /path/to/protected_dir>
Order Deny,Allow
Deny from all
<FilesMatch "^thumbs_.*">
Allow from all
</FilesMatch>
</Directory>