Today We are going to talk about how to Stop directory listing on Apache Web Server, In this post, I used my subdomain apache.hacknos.com in your case few things are different Like Domain Name, Apache VirtualHost File, and Document Root Directory.
In Apache Web Server, directory listing is a default option that displays the contents of a directory if there is already have any indexing file Such as Index.php or index.html, so your content didn’t display to any user if there is no indexing file your content is displayed publicly.
Let’s check how to display files and directories on Apache Server, navigate your Website URL and you can saw your Server Files.
Disabling Directory Listing on Apache Web Server
There are two ways to disable Apache Directory listing On Apache Server, If you’re using the default configuration host file ( 000-default.conf ) for Apache Web-server you need to edit your Apache2 configuration file.
Let’s open the file on your text editor.
sudo vi /etc/apache2/apache2.conf
And find this default code on your apache2.conf file
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
Change this line
Options Indexes FollowSymLinks
to this line
Options -Indexes +FollowSymLinks
and restart you Apache2 Service run the command
sudo service apache2 restart
Apache Disable Directory Listing Using Virtualhost
The Second Option is to disable directory listing using a Virtualhost conf file, If you’re setup multiple websites on a single Server like me, so we need to edit our virtual host file.
in my case, my Apache Virtualhost configuration file name is ( apache.hacknos.com.conf ) in your case your configuration file name is different.
let’s open this file in any editor.
sudo vi /etc/apache2/sites-enabled/apache.hacknos.com.conf
And Add this code on your Virtualhost configuration file.
<Directory change-to-your-documentroot-directory> Options Indexes </Directory>
The next step is to go to your Document-Root Directory and Create a file .htaccess and Add this code ( Options -Indexes” on your .htaccess file.
And reload your Apache2 Service
cd /var/www/apache echo "options -Indexes" >.htaccess sudo systemctl reload apache2
Again open your browser and navigate your Website URL and We saw Forbidden Resource Error. It’s mean our configuration is successfully complete.