Prevent Directory Listing using HTACCESS

PHP

Read {count} times since 2020

If you don’t want your site users to see your directory listing, you can do a simple trick in .htaccess file.
Here is a screenshot of a directory listing on Apache Server :

Now, to disable directory listing of all types of files, type in the following code in .htaccess :

IndexIgnore *

This will change the directory listing to this :

The empty directory listing looks ugly. To make it beautiful, you can add an index page for all directory listings without adding each index files on each directory. Use the following code on .htaccess for that.

DirectoryIndex index.php /primary.php

The above code will make all directory’s index file primary.php which is kept in the root directory of the site if there isn’t an index.php file in the directory. I have made a primary.php file and added it as the index file of directory and It looks like below :