XAMPP环境下PHP项目public目录的.htaccess配置问题求助
解决导航404错误的配置修正方案
1. 修正根目录(xampp/demo)的.htaccess
添加RewriteBase消除路径歧义,同时细化请求匹配规则:
# Redirect requests to public folder RewriteEngine On RewriteBase /demo RewriteCond %{REQUEST_URI} !^/demo/public/ RewriteRule ^(.*)$ public/$1 [L]
2. 修正public目录(xampp/demo/public)的.htaccess
调整重写目标路径为当前目录下的index.php,并修正RewriteBase:
# Remove index.php and question mark from the request, enabling pretty or "vanity" URLs RewriteEngine On RewriteBase /demo/public RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ index.php?$1 [L,QSA]
3. 修正httpd.conf中的虚拟主机配置
将ServerName改为自定义域名避免冲突,优化配置结构:
# php for beginners, laracasts 8/22/2023 <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/demo" ServerName phpdemo ServerAlias www.phpdemo <Directory "C:/xampp/htdocs/demo"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
4. 修改Windows hosts文件
以管理员权限打开C:\Windows\System32\drivers\etc\hosts,添加一行:
127.0.0.1 phpdemo
5. 重启XAMPP Apache服务
完成所有修改后重启Apache,访问http://phpdemo即可正常使用所有导航链接。
原理说明
- 根目录.htaccess将所有非public路径的请求转发到public目录
- public目录的.htaccess处理伪静态规则,把请求导向自身目录下的index.php
- 虚拟主机与hosts配置确保自定义域名精准指向项目,避免默认localhost的路径冲突
内容的提问来源于stack exchange,提问作者Kazz
相关产品推荐
相关产品推荐

