(转)如何防止Apache显示文件列表

当你在浏览器输入地址:
http://localhost/
如果你的文件根目录里有 index.html,浏览器就会显示 index.html的内容,如果没有 index.htmlApache将在浏览器显示文件根目录的目录列表,目录列表包括文件根目录下的文件和子目录。给网站造成安全风险。
同样当你输入一个虚拟目录的地址:
http://localhost/My/
如果该虚拟目录下没有 index.html,浏览器也会显示该虚拟目录的目录结构,列出该虚拟目录下的文件和子目录。
我们可以通过修改Apache的配置文件,来禁止 Apache 显示目录结构列表。


打开httpd.conf ,来看一个目录配置:

<Directory "/usr/local/apache/htdocs">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

你只需要将上面红色代码中的 Indexes 去掉,就可以禁止Apache 显示该目录结构。用户就不会看到该目录下的文件和子目录列表了。
Indexes 的作用就是当该目录下没有 index.html 文件时,就显示目录结构。
现改为如下:

<Directory "/usr/local/apache/htdocs">

    Options FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

另外也可以在 Indexes 前加一个减号 “-”,同样可以禁止Apache显示目录结构。
Indexes前加 “+” 代表允许目录浏览;加 “-” 代表禁止目录浏览。修改如下:

<Directory "/usr/local/apache/htdocs">

    Options -Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

原文地址:https://www.cnblogs.com/sallybin/p/3130559.html