Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable directory listings in all but one folder, using htaccess?

Tags:

.htaccess

Right now, I'm using Options -Indexes to hide access to all directory listings on a website of mine.

What I've discovered however is that I need directory access to a particular folder:

../Resource/Other/

Is there any way of applying a logical NOT rule to htaccess to allow access to certain folders, while disabling directory access by default? Or do I have to approach it from the other angle and enable directory listings globally and then selectively disable them folder by folder?

like image 828
marked-down Avatar asked Feb 01 '14 21:02

marked-down


2 Answers

Create a htaccess file with Options +Indexes in the folder you want to list.

Be sure to remove any index files too.

like image 88
Panama Jack Avatar answered Oct 13 '22 06:10

Panama Jack


the almost easiest way to do it is disabling listings globally and then allow list others. That is, your virtual host should be set up by default not to list directories when removing the "Indexes" option. Then you would add a Directory directive and set "Options" to allow listing of a particular directory.

For instance:

Say you have the following directory structure: /home/user/www (note: www is the document root).

Into www directory there are appdir1, appdir2, app3 directories and you want list only appdir3 so in your virtualost:

<VirtualHost *:80>  
    DocumentRoot /home/user/www
    ServerName myserver.local

    <Directory /home/user/www/>
        Options FollowSymLinks MultiViews   
    </Directory>
</VirtualHost>

In this case is not possible directory listing, any directories under document root is forbidden. However, if you add other Directory directive you can set directory listing to specif dir:

<VirtualHost *:80>  
    DocumentRoot /home/user/www
    ServerName myserver.local

    <Directory /home/user/www/>
        Options FollowSymLinks MultiViews   
    </Directory>

    <Directory /home/user/www/appdir3>
        Options Indexes
    </Directory>
</VirtualHost>

On the other hand you can add .htaccess files to directories that you want allow directory listing. In /home/user/www/appdir3/.htaccess add:

Options +Indexes

Also, if you runs Apache version 2.4+ you should take a look the <If> directive.

<If> Directive

like image 3
felipsmartins Avatar answered Oct 13 '22 06:10

felipsmartins