On this page
- Why is a directory listing a problem?
- How can you tell if listings are on right now?
- How do you turn listings off for the whole server?
- How do you turn them off for a single site?
- Can you do it in .htaccess instead?
- Should you unload mod_autoindex completely?
- How do you confirm the change worked?
- What else should you lock down at the same time?
- Questions
Why is a directory listing a problem?
An automatic index shows every file in a folder to anyone who requests the folder path. That often includes things nobody meant to publish: database dumps left after a migration, .bak copies of config files, old ZIP exports, upload folders full of customer files, or the internal layout of a plugin that an attacker can match against known flaws. The module responsible is mod_autoindex, and it only kicks in when a directory has no index file and the Indexes option is allowed for that path.
Both the Debian family and the Red Hat family ship Apache with Indexes turned on for the default document root. Turning it off costs nothing for normal sites, because real pages always have an index file or are routed through a front controller such as index.php.
How can you tell if listings are on right now?
Create an empty test folder in your web root and request it. If Apache returns a page titled "Index of", listings are active.
sudo mkdir -p /var/www/html/listing-test
sudo touch /var/www/html/listing-test/secret.txt
curl -s http://198.51.100.24/listing-test/ | grep -i "index of"Any output means the folder contents are visible. Find where the option is set with a quick search through the configuration tree:
# Ubuntu and Debian
grep -rn "Indexes" /etc/apache2/
# Rocky Linux and AlmaLinux
grep -rn "Indexes" /etc/httpd/Expect a hit in /etc/apache2/apache2.conf on Debian and Ubuntu (the /var/www/ block) and in /etc/httpd/conf/httpd.conf on Rocky and Alma (the /var/www/html block). Lines that already read -Indexes are fine.
How do you turn listings off for the whole server?
Edit the main <Directory> block so it removes Indexes. Using explicit plus and minus signs makes the intent obvious and avoids surprises when options merge from several blocks.
On Ubuntu and Debian, open /etc/apache2/apache2.conf and change the /var/www/ block:
<Directory /var/www/>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>On Rocky Linux 9 and AlmaLinux 9, open /etc/httpd/conf/httpd.conf and change the block for /var/www/html:
<Directory "/var/www/html">
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>Apache refuses to start if one Options line mixes signed and unsigned words, so give every option a sign or none at all. Test and reload:
# Ubuntu and Debian
sudo apache2ctl configtest && sudo systemctl reload apache2
# Rocky and Alma
sudo apachectl configtest && sudo systemctl reload httpdHow do you turn them off for a single site?
Put the Options line in the site's own virtual host when you serve from a path outside the default root or want different behaviour per site. A block inside the virtual host wins over the global one for that path.
<VirtualHost *:443>
ServerName example.net
DocumentRoot /var/www/example.net/public
<Directory /var/www/example.net/public>
Options -Indexes +FollowSymLinks
AllowOverride FileInfo
Require all granted
</Directory>
</VirtualHost>The opposite case also comes up: a downloads folder where you do want a listing. Allow it for that one path only, and keep everything else closed:
<Directory /var/www/example.net/public/downloads>
Options +Indexes
IndexOptions FancyIndexing NameWidth=* HTMLTable
</Directory>Keep that folder free of anything private, because every file dropped in becomes public at once.
Can you do it in .htaccess instead?
Yes, add Options -Indexes to an .htaccess file, but only when the virtual host lets that file change options. The directory block must contain AllowOverride Options or AllowOverride All; otherwise Apache answers with a 500 Internal Server Error because the directive is not permitted there.
# /var/www/example.net/public/.htaccess
Options -IndexesThis route suits sites that are deployed by tools you cannot change, such as a CMS that ships its own .htaccess. When you control the server, the virtual host is the better place: Apache reads it once at startup instead of checking every folder for .htaccess on each request. The LAMP stack guide shows a full virtual host for PHP apps.
Should you unload mod_autoindex completely?
Unloading the module removes the feature from the server entirely, which is a reasonable extra step when no site needs listings. On Debian and Ubuntu the tool warns that the module is marked essential, so you must force it:
sudo a2dismod -f autoindex
sudo apache2ctl configtest && sudo systemctl restart apache2On Rocky and Alma the module is loaded from /etc/httpd/conf.modules.d/00-base.conf, and its settings live in /etc/httpd/conf.d/autoindex.conf. That second file uses directives such as IndexOptions that fail without the module, so move both out of the way together:
sudo sed -i "s/^LoadModule autoindex_module/#LoadModule autoindex_module/" /etc/httpd/conf.modules.d/00-base.conf
sudo mv /etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/autoindex.conf.off
sudo apachectl configtest && sudo systemctl restart httpdPackage updates can restore the default files, so recheck after major Apache upgrades. For most servers, Options -Indexes alone is enough and survives updates without attention.
How do you confirm the change worked?
Request the test folder again and look at the status code. You want 403 and no listing.
curl -s -o /dev/null -w "%{http_code}\n" http://198.51.100.24/listing-test/
sudo rm -r /var/www/html/listing-testThen check a real page still loads with 200, and read the error log if anything changed unexpectedly: sudo tail -n 20 /var/log/apache2/error.log on Debian and Ubuntu, or sudo tail -n 20 /var/log/httpd/error_log on Rocky and Alma. A log line mentioning "Cannot serve directory" and "forbidden by Options directive" confirms Apache blocked the listing on purpose.
What else should you lock down at the same time?
Listings are one of several defaults worth tightening on a fresh web server. Quick additions that pair well:
- Hide version details: set
ServerTokens ProdandServerSignature Off(in/etc/apache2/conf-available/security.confon Debian and Ubuntu, or a new file in/etc/httpd/conf.d/on Rocky and Alma). - Block hidden files: deny requests for paths starting with a dot, apart from
.well-known, so a stray.gitor.envcannot leak. - Keep backups out of the web root: store dumps and archives in your home folder, never under
/var/www. - Force HTTPS: follow the HTTPS and www redirect guide.
<DirectoryMatch "/\.(?!well-known)">
Require all denied
</DirectoryMatch>Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Turn off Apache directory listings
Why do I get 403 on my home page after this change?
The folder has no file named in DirectoryIndex. Add an index.html or index.php, or set DirectoryIndex to the file your app uses as its entry point.
Does Nginx list directories by default?
No. Nginx only lists folders when autoindex on; is set for a location, so there is nothing to turn off unless someone enabled it.
Will disabling listings break WordPress or Laravel?
No. Both route requests through an index file, so normal pages are unaffected. Upload and plugin folders simply stop showing their contents.
Is a 403 better than a 404 here?
Either hides the files. A 403 is what Apache returns by default and makes it clear in your logs why the request failed. If you prefer not to reveal that the folder exists, add a rule that answers with 404 for those paths.
Do I need to restart or just reload?
A reload is enough for Options changes. Loading or unloading a module, such as mod_autoindex, needs a full restart.
More in Web servers and proxies
- Install and configure Nginx
- Install and configure Apache
- OpenLiteSpeed web server
- HAProxy load balancer and TLS proxy
- Load balancing with Nginx upstreams
- Nginx as a reverse proxy for an app
- Apache as a reverse proxy
- Compression with gzip and Brotli
- HTTPS and www redirects
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Apache Tomcat for Java apps
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site