Dino Geek, try to help you

How to prevent directory listing?


Preventing directory listing is crucial to maintain the security and privacy of the files hosted on your web server. Directory listing occurs when a web server does not find an index file (like `index.html` or `index.php`) in a directory, and then automatically generates and serves a listing of the directory’s contents to the web browser. This can expose sensitive files or confidential structures to unauthorized users. Here are several methods to prevent directory listing, along with detailed explanations and examples.

1. Use `.htaccess` File in Apache Servers: Apache servers allow configuration through `.htaccess` files. To prevent directory listing, you can add the following directive to the `.htaccess` file in the root directory of your website or in any specific directory:

\`\`\` Options -Indexes \`\`\` This directive turns off indexing, preventing the server from displaying the contents of directories without an index file. For instance, if you place this line in the `.htaccess` file of the directory `/var/www/html/private`, trying to access `http://yourdomain.com/private/` will result in a “403 Forbidden” error instead of a directory listing. Example: \`\`\`htaccess # Prevent directory listing Options -Indexes \`\`\` Source: - [Apache .htaccess Tutorial](https://httpd.apache.org/docs/current/howto/htaccess.html)

1. Modify Nginx Configuration: For Nginx servers, directory listing can be disabled by modifying the server configuration file, usually found at `/etc/nginx/nginx.conf` or within a site’s server block configuration file, often located in `/etc/nginx/sites-available/`. Add or edit the `autoindex` directive as follows:

\`\`\`nginx server { … location / { autoindex off; } … } \`\`\` Setting `autoindex` to `off` disables directory listings for the specified location block. Example: \`\`\`nginx server { listen 80; server\_name yourdomain.com; location / { root /var/www/html/; index index.html index.htm; autoindex off; } } \`\`\` Source: - [Nginx Documentation on autoindex Directive](https://nginx.org/en/docs/http/ngx_http_autoindex_module.html#autoindex)

1. Use Web Server Configuration Settings: Many web servers, such as Apache and Nginx, also allow directory listing settings to be configured globally in the main configuration files.

For Apache: Edit the `httpd.conf` file, often located in `/etc/httpd/conf/`, and ensure the following is set: \`\`\`apache Options -Indexes \`\`\` For Nginx: Edit the respective server block configuration file and ensure `autoindex` is set to `off`. Source: - [Apache Module mod_autoindex](https://httpd.apache.org/docs/2.4/mod/mod_autoindex.html)

1. Default Index Files: Ensure that each directory on your server contains a default index file like `index.html`, `index.htm`, `index.php`, etc. This practice prevents the web server from listing directory contents, as it will serve the index file instead.

Example: Create an `index.html` file with the following content in every directory you want to protect: \`\`\`html Access Denied

403 Forbidden

Access to this directory is denied.

\`\`\`

1. Web Hosting Control Panels: If you are using web hosting control panels like cPanel, Plesk, or Webmin, these typically have options to disable directory listing via graphical user interfaces, allowing non-technical users to secure their directories without manual file editing.

Source: - [cPanel Documentation on Indexes](https://docs.cpanel.net/cpanel/file-manager/indexes/)

By implementing these measures, you can effectively prevent directory listing on your web server, enhancing the security and confidentiality of your web content.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use