編輯 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 | 精細控制可覆蓋類別(更安全) | 進階用法 |
systemctl restart httpd 或 apachectl -k restart。.htaccess:
RewriteEngine On
RewriteRule ^test\.html$ /index.html [L]
https://your-domain/test.html 若跳轉至首頁,即生效。<Directory /var/www/html> AllowOverride All </Directory>,避免全域開放。<Files ".htaccess">
Require all denied
</Files>
| 現象 | 原因 | 解決 |
|---|---|---|
| 修改後不生效 | 配置檔語法錯誤、未重啟、被較深層 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 成為輕量級應用層網關的利器。