Apache + mod_rewrite 启用伪静态第一关是确认mod_rewrite模块是否加载:打开httpd.conf文件,搜索LoadModule rewrite_module,确保该行未被注释(即前面无#号),否则.htaccess将被忽略。
本地 PHP 环境是否支持伪静态,第一关是看 mod_rewrite 模块有没有加载。很多一键环境(如 XAMPP、WampServer)默认不启用它,导致 .htaccess 文件完全被忽略。
httpd.conf(路径通常在 XAMPP\apache\conf\httpd.conf 或 Wamp\bin\apache\apache{version}\conf\httpd.conf)LoadModule rewrite_module,确保该行未被注释(即前面没有 #),找到你项目所在的目录段(比如 ),确认其中包含这两行:AllowOverride All
Require all granted
(旧版 Apache 2.2 是 Order Allow,Deny + Allow from all)
phpinfo() 页面检查 Loaded Modules 是否含 mod_rewrite
即使模块开了,.htaccess 仍可能不生效——最常踩的坑是规则语法错误或匹配逻辑没对上。
.htaccess(注意开头的点,Windows 资源管理器可能隐藏扩展名,建议用编辑器另存为)/article/123 映射到 /index.php?id=123):RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/(\d+)$ index.php?id=$1 [L]RewriteBase 很关键:如果项目不在 Apache 根目录(比如放在 localhost/myapp/),这里要改成 RewriteBase /myapp/,否则重写路径会错位如果你用的是 php -S localhost:8000 启动的内置服务器,那直接放弃 .htaccess ——它压根不读这个文件,也不支持 mod_rewrite。
$_SERVER['REQUEST_URI'],自行分发请求)在 Windows 上配伪静态,有两个隐

.htaccess 文件权限:Apache 进程需有读取权限。若用管理员权限启动 Apache 但文件属主是普通用户,有时会静默失败。建议右键文件 → 属性 → 安全 → 给 Everyone 或 Users 组添加“读取”权限httpd.conf 中添加:AllowEncodedSlashes On并重启服务
.htaccess 的读取,临时关闭试试RewriteBase 和 Windows 下的文件权限,调不通时先盯住这两处。