天天看點

如何防止Apache顯示檔案清單

當你在浏覽器輸入位址:

http://localhost:8080/

如果你的檔案根目錄裡有 index.html,浏覽器就會顯示 index.html的内容,如果沒有 index.html,apache将在浏覽器顯示檔案根目錄的目錄清單,目錄清單包括檔案根目錄下的檔案和子目錄。給網站造成安全風險。

同樣當你輸入一個虛拟目錄的位址:

http://localhost:8080/my/

如果該虛拟目錄下沒有 index.html,浏覽器也會顯示該虛拟目錄的目錄結構,列出該虛拟目錄下的檔案和子目錄。

我們可以通過修改apache的配置檔案,來禁止 apache 顯示目錄結構清單。

打開httpd.conf ,來看一個目錄配置:

<directory "d:/wamp/www">

     options indexes followsymlinks

     allowoverride none

     order allow,deny

     allow from all

</directory>

你隻需要将上面紅色代碼中的 indexes 去掉,就可以禁止apache 顯示該目錄結構。使用者就不會看到該目錄下的檔案和子目錄清單了。

indexes 的作用就是當該目錄下沒有 index.html 檔案時,就顯示目錄結構。

現改為如下:

     options followsymlinks

另外也可以在 indexes 前加一個減号 “-”,同樣可以禁止apache顯示目錄結構。

在indexes前加 “+” 代表允許目錄浏覽;加 “-” 代表禁止目錄浏覽。修改如下:

     options -indexes followsymlinks