在 Windows 下使用 DedeAMPZ(Apache + MySQL + PHP 一鍵安裝包)搭建開發環境時,預設 Apache 未開啟 mod_rewrite 或目錄覆蓋權限,導致框架/CMS 的偽靜態規則不生效。本文提供最小化配置步驟,快速啟用偽靜態。
httpd.conf找到 Apache 安裝目錄下的 conf/httpd.conf,定位 <Directory /> 區塊(或你的網站根目錄對應的 <Directory>),修改為:
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
| 指令 | 作用 |
|---|---|
Options FollowSymLinks | 允許跟隨符號連結,rewrite 依賴此項 |
AllowOverride All | 允許 .htaccess 覆蓋配置,偽靜態生效的關鍵 |
Require all granted | Apache 2.4+ 授權語法(舊版用 Order allow,deny / Allow from all) |
mod_rewrite 模組在 httpd.conf 中搜尋並取消註釋:
LoadModule rewrite_module modules/mod_rewrite.so
重啟 Apache 使配置生效。
.htaccess:RewriteEngine On
RewriteRule ^test\.html$ index.php [L]
index.php:<?php echo 'Rewrite OK!'; ?>https://localhost/test.html,若顯示 "Rewrite OK!" 則配置成功。DocumentRoot 所在的 <Directory>。httpd.conf 後面的 VirtualHost 或額外 include 的配置可能覆蓋前面的設置,檢查完整配置鏈。AllowOverride None,務必手動改為 All。若使用虛擬主機,建議在 httpd-vhosts.conf 中單獨配置:
<VirtualHost *:80>
DocumentRoot "D:/www/mysite"
ServerName mysite.local
<Directory "D:/www/mysite">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
同時在 hosts 增加 127.0.0.1 mysite.local,實現多項目隔離開發。
適用環境:DedeAMPZ / XAMPP / WAMP / 手動安裝 Apache 2.4+