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.
Apache Stop Directory Listing Related Posts
- Apache Server Password Protected
- Free SSL For Apache Server Website
- Apache Stop Directory Listing
- Joomla Install on Apache Server
- Apache Stop Directory Listing On Ubuntu Server
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 listing
Let’s check how to display files and directories on Apache Server, navigate your Website URL and you can saw your Server Files.
1 |
http://apache.hacknos.com |
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.
1 |
sudo vi /etc/apache2/apache2.conf |
And find this default code on your apache2.conf file
1 2 3 4 5 |
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> |
Change this line
1 |
Options Indexes FollowSymLinks |
to this line
1 |
Options -Indexes +FollowSymLinks |
and restart your Apache2 Service run the command
1 |
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.
1 |
sudo vi /etc/apache2/sites-enabled/apache.hacknos.com.conf |
And Add this code to your Virtualhost configuration file.
1 2 3 |
<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
1 2 3 |
cd /var/www/apache echo "options -Indexes" >.htaccess sudo systemctl reload apache2 |
Apache Stop Directory Listing Successfully Disable
Again open your browser and navigate your Website URL and We saw Forbidden Resource Error. It’s mean our configuration is successfully complete.
1 |
http://apache.hacknos.com |
Common Question People Ask Related to Apache Stop Directory Listing
- 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 follow these steps. An example I have a Private Name Directory and I want to restrict it 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.
1 |
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.
1 |
sudo vi /etc/apache2/sites-enabled/000-defautl.conf |
And Add this code to your virtual-host file
1 2 3 4 5 6 7 8 |
<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 looks like this image. edit the configuration code you according.
Go to your Private Directory and Create a .htaccess File
1 2 |
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.
1 2 3 4 |
AuthType Basic AuthName "your comment here" AuthUserFile <your passwd file location> Require Valid-user |
More Related Post Apache Stop Directory Listing
- File Transfer Using Apache Server
- Apache VirtualHost Overview
- Apache Stop Directory Listing
- Onion Dark Web Website Host on Apache Server
- Disable Directory listing
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 in some cases, you put the .htaccess in a file in another directory.
What is Apache FollowSymLinks
Apache FollowSymLinks use for Creating a symlink file and Directory to pointing to your webroot.