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.
What are directory listings
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.
How do I stop directory listing in Apache
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.
Common Question People Ask
- How do I stop directory listing in Apache,
- How do I turn off directory listing
- How do I restrict access to Apache Directory
- Where can I find .htaccess file in Apache
- Where do I put .htaccess file
- What is Apache FollowSymLinks
- How do I create a .htaccess file
How do I restrict access to Apache Directory
If you want to Restrict the directory on Apache Server follows these steps. An example I have a Private Name Directory and I want to restricted to Access Publicly.
Let’s connect with your ssh service and run the add a user command for password Authentication in the Private Directory.
( -c ) where You want to Save .htpasswd File You can choose any location for this File.
sudo htpasswd -c /etc/apache2/.htpasswd <username>
After creating our new user and password for the private directory we move to the next step Let’s open your Apache configuration file in any editor. if you’re using Apache Virtual-host File in your case your virtual-host file name is different.
sudo vi /etc/apache2/sites-enabled/000-defautl.conf
And Add this code on your virtual-host file
<Directory "your Directory location here"> AuthType Basic AuthName "your Comment here" AuthUserFile <your password location > Require valid-user </Directory>
After Insert the code on your Virtual-host file your file is look like this image. edit the configuration code your according.
Go to your Private Directory and Create a .htaccess File
cd /var/www/html/Private/ sudo touch .htaccess
and add this code on your .htaccess file and restart your Apache2 Service and Reload Your Private Directory.
AuthType Basic AuthName "your comment here" AuthUserFile <your passwd file location> Require Valid-user
Where can I find the .htaccess file in Apache
You can locate your .htaccess file in your Document Root Directory, but it’s hidden you can visible the file by changing your Setting.
Where do I put .htaccess file
You can place your .htaccess file in your Document Root Directory. but some case you put the .htaccess file other directory.
What is Apache FollowSymLinks
Apache FollowSymLinks use for Creating a symlink file and Directory to pointing to your webroot.