Stop Apache from showing directory contents when no index file exists.
Disable Directory Browsing
Add to .htaccess
:
Options -Indexes
That's it.
What This Prevents
Without this, visiting example.com/uploads/
shows:
Index of /uploads [ICO] Name Last modified Size [DIR] 2024/ 2024-01-15 10:23 - [IMG] secret.jpg 2024-01-12 09:15 2.1M [TXT] passwords.txt 2024-01-10 14:22 1.2K
With this, visitors get:
403 Forbidden
Variations
Disable for specific directories:
<Directory /var/www/html/uploads> Options -Indexes </Directory>
Custom error message:
Options -Indexes ErrorDocument 403 "Access denied"
Hide only certain files:
IndexIgnore *.pdf *.doc *.zip
Enable for Specific Folders
If you need directory listing somewhere:
# In specific folder's .htaccess Options +Indexes
Check Current Status
curl -I https://example.com/uploads/
Returns 403 = disabled, 200 = enabled