當網站目錄下缺少預設首頁(如 index.html、index.php)時,Apache 預設會列出該目錄所有檔案與子目錄,這屬於資訊洩漏風險。本文說明三種關閉目錄索引的方法,管理員可依部署架構選擇適合方案。
Apache 的 Options 指令中包含 Indexes 選項,控制是否允許目錄列表。移除或加上減號(-Indexes)即可禁用。
<Directory "D:/Apache/blog.example.com">
Options FollowSymLinks # 移除 Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# 找到原本的
Options Indexes FollowSymLinks
# 修改為
Options -Indexes FollowSymLinks
或在虛擬主機區塊中單獨設定:
<VirtualHost *>
<Directory "../vhosts/blog.example.com">
Options -Indexes FollowSymLinks
</Directory>
ServerAdmin admin@example.com
DocumentRoot "../vhosts/blog.example.com"
ServerName blog.example.com
...
</VirtualHost>
+Indexes = 允許目錄瀏覽-Indexes = 禁止目錄瀏覽在網站根目錄建立或編輯 .htaccess:
<Files *>
Options -Indexes
</Files>
AllowOverride Options 或 AllowOverride All 已在上層目錄啟用https://example.com/images/)https://example.com/images/photo.jpg)ErrorDocument 403 /error/403.html)提升使用者體驗