deny directory listing with htaccess


Apache Problem Overview

I have a folder, for example : /public_html/Davood/ and too many sub folder in folder, for example : /public_html/Davood/Test1/ , /public_html/Davood/Test1/Test/ , /public_html/Davood/Test2/ , ...

I want add a htaccess file into /public_html/Davood/ To deny DirectoryListing In /Davood And Sub Folders, It's Possible ?

Apache Solutions

Solution 1 - Apache

Options -Indexes should work to prevent directory listings.

If you are using a .htaccess file make sure you have at least the "allowoverride options" setting in your main apache config file.

Solution 2 - Apache

Try adding this to the .htaccess file in that directory.

Options -Indexes

This has more information.

Solution 3 - Apache

If Options -Indexes does not work as Bryan Drewery suggested, you could write a recursive method to create blank index.php files.

Place this inside of your base folder you wish to protect, you can name it whatever (I would recommend index.php)



function recurse($path){
    foreach(scandir($path) as $o){
        if($o != "." && $o != ".."){
            $full = $path . "/" . $o;
                if(!file_exists($full . "/index.php")){
                    file_put_contents($full . "/index.php", "");


These blank index.php files can be easily deleted or overwritten, and they'll keep your directories from being listable.

Solution 4 - Apache

For showing Forbidden error then include these lines in your .htaccess file:

Options -Indexes 

If we want to index our files and showing them with some information, then use:

IndexOptions -FancyIndexing

If we want for some particular extension not to show, then:

IndexIgnore *.zip *.css

Solution 5 - Apache

Options -Indexes perfectly works for me ,

here is .htaccess file :

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes <---- This Works for Me :)

   ....etc stuff


Before : enter image description here

After :

enter image description here

Solution 6 - Apache

There are two ways :

  1. using .htaccess : Options -Indexes

  2. create blank index.html

Solution 7 - Apache

Options -Indexes

I have to try create .htaccess file that current directory that i want to disallow directory index listing. But, sorry i don't know about recursive in .htaccess code.

Try it.

Solution 8 - Apache

Agree that

Options -Indexes

should work if the main server is configured to allow option overrides, but if not, this will hide all files from the listing (so every directory appears empty):

IndexIgnore *

Solution 9 - Apache

Options -Indexes returns a 403 forbidden error for a protected directory. The same behaviour can be achived by using the following Redirect in htaccess :

RedirectMatch 403 ^/folder/?$ 

This will return a forbidden error for .

You can also use mod-rewrite to forbid a request for folder.

RewriteEngine on

RewriteRule ^folder/?$ - [F]

If your htaccess is in the folder that you are going to forbid , change RewriteRule's pattern from ^folder/?$ to ^$ .


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDJafariView Question on Stackoverflow
Solution 1 - ApachePPC-CoderView Answer on Stackoverflow
Solution 2 - ApacheBryan DreweryView Answer on Stackoverflow
Solution 3 - ApacheCycloneView Answer on Stackoverflow
Solution 4 - ApacheVivekView Answer on Stackoverflow
Solution 5 - ApacheSaurabh MistryView Answer on Stackoverflow
Solution 6 - ApacheCakkaView Answer on Stackoverflow
Solution 7 - ApacheJersey WilliamView Answer on Stackoverflow
Solution 8 - ApachedashrbView Answer on Stackoverflow
Solution 9 - ApacheAmit VermaView Answer on Stackoverflow