Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deny directory listing with htaccess

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 ?

like image 296
DJafari Avatar asked May 09 '11 04:05

DJafari


People also ask

How do I deny access to htaccess?

Create a . htaccess file in the root of your project directory structure. Then open the . htaccess file and write this directive deny from all.


9 Answers

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.

like image 94
PPC-Coder Avatar answered Oct 03 '22 22:10

PPC-Coder


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

Options -Indexes

This has more information.

like image 27
Bryan Drewery Avatar answered Oct 03 '22 23:10

Bryan Drewery


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)

<?php

recurse(".");

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

?>

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

like image 35
Cyclone Avatar answered Oct 03 '22 21:10

Cyclone


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
like image 21
Vivek Avatar answered Oct 03 '22 22:10

Vivek


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 :)
    </IfModule>


   ....etc stuff

</IfModule>

Before : enter image description here

After :

enter image description here

like image 29
Saurabh Mistry Avatar answered Oct 03 '22 22:10

Saurabh Mistry


There are two ways :

  1. using .htaccess : Options -Indexes

  2. create blank index.html

like image 38
Cakka Avatar answered Oct 03 '22 22:10

Cakka


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.

like image 37
Jersey William Avatar answered Oct 03 '22 21:10

Jersey William


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 *
like image 26
dashrb Avatar answered Oct 03 '22 23:10

dashrb


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 example.com/folder/ .

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 ^$ .

like image 33
Amit Verma Avatar answered Oct 03 '22 23:10

Amit Verma