Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Apache 禁止顯示目錄索引的三種常用方法詳解

編輯:關於伺服器

Apache 目錄瀏覽功能關閉完整指南

當網站目錄下缺少預設首頁(如 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>
  • 精準控制單一虛擬主機或特定路徑
  • 不影響其他網站目錄
  • 修改後需重啟 Apache 生效

方法二:修改主設定檔 httpd.conf(全域生效)

# 找到原本的
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 檔案(無主機管理權限時適用)

在網站根目錄建立或編輯 .htaccess:

<Files *>
    Options -Indexes
</Files>
  • 需確認 AllowOverride Options 或 AllowOverride All 已在上層目錄啟用
  • 適合虛擬主機用戶無法修改 httpd.conf 的情況
  • 每次請求會讀取 .htaccess,微量影響效能

驗證方式

  1. 瀏覽器訪問無首頁的目錄網址(如 https://example.com/images/)
  2. 若顯示 403 Forbidden 或 404 Not Found 代表成功
  3. 若仍顯示檔案列表,檢查設定語法並重啟 Apache

補充說明

  • 關閉目錄索引不影響直接存取已知檔案路徑(如 https://example.com/images/photo.jpg)
  • 建議同時設定自訂錯誤頁面(ErrorDocument 403 /error/403.html)提升使用者體驗
  • 生產環境應定期掃描確認無非預期目錄瀏覽洩漏
Copyright © Windows教程網 All Rights Reserved