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

Apache 啟用 .htaccess 支援:httpd.conf 核心配置一步到位

編輯:關於伺服器

核心配置修改

編輯 Apache 配置檔 httpd.conf(位於 {Apache根目錄}/conf/),找到根目錄區塊:

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

將 AllowOverride None 修改為 AllowOverride All:

<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>

參數詳解

指令含義建議值
Options FollowSymLinks允許跟隨符號連結保留
AllowOverride All允許 .htaccess 覆蓋所有可覆蓋指令必改
AllowOverride None禁止 .htaccess(預設,安全但不靈活)生產環境若無需求可保留
AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,MultiViews精細控制可覆蓋類別(更安全)進階用法

生效驗證

  1. 重啟 Apache:systemctl restart httpd 或 apachectl -k restart。
  2. 在網站根目錄建立測試 .htaccess:
    RewriteEngine On
    RewriteRule ^test\.html$ /index.html [L]
  3. 訪問 https://your-domain/test.html 若跳轉至首頁,即生效。

安全加固建議

  • ✅ 僅在必要目錄開啟:<Directory /var/www/html> AllowOverride All </Directory>,避免全域開放。
  • ✅ 禁止 .htaccess 被直接下載:
    <Files ".htaccess">
        Require all denied
    </Files>
  • ✅ 定期審計 .htaccess 內容,防範惡意重寫、竊取 Cookie、釣魚跳轉。

常見問題排查

現象原因解決
修改後不生效配置檔語法錯誤、未重啟、被較深層 Directory 覆蓋apachectl -t 檢查語法、確認重啟、檢查子目錄配置
500 Internal Server Error.htaccess 語法錯誤、指令不被允許查 error_log、縮小 AllowOverride 範圍排查
權限拒絕Apache 進程無讀取 .htaccess 權限chown -R apache:apache /var/www/html

總結

一行 AllowOverride All 啟用 .htaccess 無限可能:URL 重寫、認證授權、快取控制、安全防護、PHP 參數覆蓋。原則:按需開啟、最小權限、嚴格審計。配合 mod_rewrite、mod_headers、mod_expires,.htaccess 成為輕量級應用層網關的利器。

Copyright © Windows教程網 All Rights Reserved