XAMPP本地WP站点PHP首页404及页面跳转异常问题求助
Windows 11下XAMPP搭建本地WordPress站点问题汇总
我的开发站点根目录为C:/xampp/htdocs/test/,XAMPP安装正常(端口80无冲突,数据库导入成功),但遇到以下问题:
- 可正常访问的地址:
http://localhosthttp://localhost/phpmyadmin/index.php
- PHP首页404:
http://localhost/test/或http://localhost/test/index.php - HTML首页可正常访问:
http://localhost/test/index.html(已放入根目录测试) - 分类页面重定向到
http://localhost/dashboard/:http://localhost/test/cat/services/
尝试过的配置(无效,甚至导致部分页面重定向到线上网站首页):
- 配置
C:\xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/test/" ServerName web1.localhost </VirtualHost>
- 修改
C:\Windows\System32\drivers\etc\hosts:
127.0.0.1 localhost 127.0.0.1 web1.localhost
解决步骤
1. 调整Apache的文件解析优先级与目录权限
问题核心是Apache优先解析index.html而非index.php,且站点目录权限配置不全:
- 打开
C:\xampp\apache\conf\httpd.conf,找到DirectoryIndex行,将index.php移至最前:DirectoryIndex index.php index.html index.htm - 在
httpd.conf末尾添加站点目录的权限配置:<Directory "C:/xampp/htdocs/test/"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted </Directory> - 重启Apache服务。
2. 修正WordPress的站点URL配置
线上数据库导入后,URL仍指向线上地址,导致重定向异常:
- 打开phpMyAdmin,找到对应WordPress数据库,执行SQL替换URL(替换
你的线上域名为实际地址):UPDATE wp_options SET option_value = replace(option_value, 'https://你的线上域名', 'http://localhost/test') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'https://你的线上域名', 'http://localhost/test'); UPDATE wp_posts SET post_content = replace(post_content, 'https://你的线上域名', 'http://localhost/test'); UPDATE wp_postmeta SET meta_value = replace(meta_value, 'https://你的线上域名', 'http://localhost/test'); - 或直接修改站点根目录的
wp-config.php,添加以下两行:define('WP_HOME','http://localhost/test'); define('WP_SITEURL','http://localhost/test');
3. 完善虚拟主机配置(可选,推荐使用)
之前的虚拟主机配置缺少基础参数,导致失效:
- 打开
httpd.conf,找到#Include conf/extra/httpd-vhosts.conf,去掉前面的#注释,开启虚拟主机功能。 - 修改
httpd-vhosts.conf,补充完整配置:# 默认localhost站点配置 <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs" ServerName localhost </VirtualHost> # 本地WordPress站点配置 <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/test/" ServerName web1.localhost <Directory "C:/xampp/htdocs/test/"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted </Directory> </VirtualHost> - 保存后重启Apache,访问
http://web1.localhost测试。
4. 修复固定链接与重定向问题
分类页面重定向到dashboard通常是固定链接或.htaccess配置异常:
- 若能登录WordPress后台,进入「设置」→「固定链接」,先选择默认选项保存,再重新选择目标固定链接结构保存,自动生成正确的
.htaccess文件。 - 若无法登录后台,手动在
C:/xampp/htdocs/test/创建.htaccess文件,写入以下内容:# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /test/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /test/index.php [L] </IfModule> # END WordPress - 确保
httpd.conf中已开启mod_rewrite模块:找到#LoadModule rewrite_module modules/mod_rewrite.so,去掉前面的#注释,重启Apache。
内容的提问来源于stack exchange,提问作者James Black
相关产品推荐
相关产品推荐

