地址栏显示localhost却加载线上WordPress站点,求排查方案
解决思路
补全/etc/hosts的localhost映射
当前你的hosts文件缺少127.0.0.1 localhost的映射,添加这一行:127.0.0.1 localhost 127.0.1.1 dara-HP-Pavilion-Notebook-15-bc5xxx保存后刷新DNS缓存(Ubuntu/Debian可执行
sudo systemctl restart systemd-resolved),确保localhost正确指向本地回环地址。确认wp-config-local.php被正确加载
检查主wp-config.php文件末尾是否有引入wp-config-local.php的代码:if (file_exists(dirname(__FILE__) . '/wp-config-local.php')) { require_once(dirname(__FILE__) . '/wp-config-local.php'); }若没有这段代码,wp-config-local.php里的配置不会生效,需手动添加。
检查WordPress数据库中的站点地址配置
即使wp-config里设置了WP_HOME和WP_SITEURL,数据库旧值仍可能干扰:- 登录本地数据库,找到WordPress选项表(默认是
wp_options) - 将
option_name为siteurl和home的option_value修改为http://localhost(若有自定义端口则加上,比如http://localhost:8080)
- 登录本地数据库,找到WordPress选项表(默认是
清除浏览器HSTS缓存
普通缓存清理无法清除HSTS强制跳转规则:- Chrome:地址栏输入
chrome://net-internals/#hsts,在「Delete domain security policies」中输入线上域名,点击「Delete」 - Firefox:输入
about:config,搜索network.stricttransportsecurity.preloadlist,临时设置为false,重启浏览器后再改回true
- Chrome:地址栏输入
检查Apache虚拟主机配置
确认Apache虚拟主机配置文件(通常在/etc/apache2/sites-available/)中,DocumentRoot指向本地代码仓库目录:<VirtualHost *:80> ServerName localhost DocumentRoot /path/to/your/local/repo <Directory /path/to/your/local/repo> AllowOverride All Require all granted </Directory> </VirtualHost>确认该虚拟主机已启用(执行
sudo a2ensite your-config.conf),并重启Apache。排查代码中的硬编码地址
搜索本地代码仓库,查找是否有硬编码的线上站点地址:grep -r "https://your-online-domain.com" /path/to/your/local/repo重点检查主题
functions.php、插件文件、静态资源引用等,替换为本地地址。
内容的提问来源于stack exchange,提问作者Myuser03
相关产品推荐
相关产品推荐

