Ugacomp

How can I Disable Directory Listing in Apache?

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Disabling directory listing in Apache is important for security reasons, as it prevents users from being able to see the contents of directories on your web server. Here’s how you can disable directory listing:

Method 1: Using .htaccess

To disable directory listing using the .htaccess file for both global and specific directories, you can create or edit the .htaccess file in the relevant directories. Here’s how you can do it:

Disable Directory Listing Globally

To disable directory listing for all directories on your Apache server, you can place the following directive in the main .htaccess file or in the server configuration file (httpd.conf):

Options -Indexes

Disable Directory Listing for Specific Directory

To disable directory listing for a specific directory, navigate to that directory and create or edit the .htaccess file. Add the following line:

Options -Indexes


Make sure AllowOverride is set to FileInfo

Ensure that the Apache server configuration allows the use of .htaccess files by setting the AllowOverride directive to at least FileInfo. This can typically be done in the main server configuration or virtual host configuration. Example in a virtual host configuration:

   <Directory "/path/to/your/web/root">
       AllowOverride FileInfo
       # other directives...
   </Directory>

Method 2: Using Apache Configuration

If you have access to the main Apache configuration file (typically named httpd.conf or apache2.conf), you can disable directory listing globally or for specific directories. Here’s how:

Open the Apache configuration file

Use a text editor to open the Apache configuration file. This file is usually located in the Apache configuration directory.

sudo nano /etc/apache2/apache2.conf 

or

sudo nano /etc/httpd/conf/httpd.conf  

Locate the Directory directive

Find the <Directory> directive that corresponds to the directory for which you want to disable directory listing.

Inside the <Directory> block, add or modify the Options directive to include “-Indexes”:

   <Directory /path/to/your/directory>
       Options -Indexes
       # other directives...
   </Directory>

Replace “/path/to/your/directory” with the actual path.

Save the changes and restart Apache to apply the configuration.

sudo service apache2 restart 

or

sudo systemctl restart httpd 

After following either method, directory listing should be disabled for the specified directory or globally on your Apache server.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.