Petar
Petar

Reputation: 37

.htaccess rule FilesMatch to all subfolders (subfolders was randomly created)

I tried to find solution here, but I don't. I working with htaccess every day but I have one problem which I can't solve.

I have directory structure:

/cmd/user_files/**[random_folder]**/avatars/pic.jpg

In /cmd/user_files/ I have more folders which randomly created over some app. Under every that randomly created folders I have some files and pictures which I don't want to access from public, and I have one subfolder "avatars" for which I want to show only .jpg

I created .htaccess in /cmd/user_files/ and I added this:

order allow,deny
deny from all
allow from 127.0.0.1

With this I deny access to every file and subfolder under /cmd/user_files/.

Now, what I need to add to this .htaccess that I can show .jpg from

/cmd/user_files/[random_folder]/avatars/*.jpg

Upvotes: 1

Views: 654

Answers (1)

MrWhite
MrWhite

Reputation: 45829

Instead of using mod_auth... it may be easier to use mod_rewrite instead. For example, in the /cmd/user_files/.htaccess file:

RewriteEngine On
RewriteRule !^[^/]+/avatars/.*\.jpg$ - [F]

This blocks (403 Forbidden) access to all files/folders except the URL-path that matches the regex ^[^/]+/avatars/.*\.jpg$ (relative to the /cmd/user_files/ subdirectory).

UPDATE: Note the ! prefix on the RewriteRule pattern - this negates the regex. This is an Apache operator, it's not part of the regex itself.

Side note... order allow,deny are Apache 2.2 directives. If you are on Apache 2.4+ then there is a different syntax.

Upvotes: 1

Related Questions