Yan Zhuang
Yan Zhuang

Reputation: 486

htaccess - Block access to a directory but allow access to files in a specific directory

In my server I have folder called customers, in which it contains sub-folders [uid] (/customers/[uid]) where uid is the unique identifier assigned to each customer. There are other folders in each /customers/[uid].

Among them there is a folder titled images which, as the name suggests, contains images.

I would like to configure the .htaccess so that the user can only access directly files in the /customers/[uid]/images but the user is not allowed to access any other folder or any directory listing.

Because I am not very familiar with .htaccess syntax, I tried to search some answers online but I am still very confused. Especially giving that I have already a RewriteRule in my .htaccess. I am not sure where and how to add more to accomplish what I want.

This is what my .htaccess look like for now.

DirectoryIndex test.php
<Files "database.ini">  
  Order Allow,Deny
  Deny from all
</Files>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

</IfModule>

I am using a VPS hosting, and the directory index is , by default, enabled. So now if I try to access /customers/user it will give me a list of folders where I can continue to access other folders and files

I have tried putting Options -Indexes inside the <IfModule>. After that, I indeed cannot access other directories, but I can't access any files in any directory anymore.

Upvotes: 0

Views: 893

Answers (1)

emadelbieh
emadelbieh

Reputation: 356

What you an do is make a .htaccess file per every user directory and inside it allow access to ./images So you will have

/customers/[uid1]/.htaccess
/customers/[uid2]/.htaccess 

... etc

and inside every .htaccess you will have:

Allow from all

Upvotes: 1

Related Questions