marked-down
marked-down

Reputation: 10408

Disable directory listings in all but one folder, using 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?

Upvotes: 7

Views: 7994

Answers (2)

felipsmartins
felipsmartins

Reputation: 13549

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

Upvotes: 3

Panama Jack
Panama Jack

Reputation: 24458

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

Be sure to remove any index files too.

Upvotes: 10

Related Questions