想在本地 Apache 環境使用 .htaccess 進行 URL 重寫、存取控制或快取設定?只需修改 httpd.conf 一處關鍵設定即可啟用。本文提供最精簡、可直接套用的操作步驟。
httpd.conf(位於 Apache 安裝目錄下的 conf/ 資料夾)。<Directory /> 區塊(或你的網站根目錄對應的 <Directory> 區塊)。AllowOverride None 改為 AllowOverride All。<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
AllowOverride FileInfo Limit,降低安全風險。httpd -k restart 或系統服務管理器重啟)。<Directory> 區塊(需對應實際 DocumentRoot)。mod_rewrite 模組未載入(檢查 LoadModule rewrite_module modules/mod_rewrite.so 未被註解)。在網站根目錄建立 .htaccess:
RewriteEngine On
RewriteRule ^test$ index.php [L]
瀏覽器訪問 https://localhost/test 若正常重寫至 index.php 即表示啟用成功。
完成上述步驟,你的 Apache 即可完整支援 .htaccess 所有功能,為框架路由、偽靜態、安全防護等提供基礎支援。