You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress站点example.com/en/wp-admin重定向丢失/en前缀至example.com/wp-admin的问题求助

Fix WordPress wp-admin Redirect Losing /en Prefix in Nginx Reverse Proxy Setup

我之前也碰到过一模一样的问题!WordPress后台的重定向逻辑在反向代理+子路径的场景下很容易踩坑,核心原因是它依赖服务器传递的头信息判断当前站点地址,咱们一步步来解决:

1. Fix Main Server Nginx Configuration (Critical for Redirects)

WordPress后台返回的重定向会指向en.example.com/wp-admin,我们需要让主服务器的Nginx自动把这个地址替换成example.com/en/wp-admin。修改你的主服务器Nginx配置片段:

server {
    server_name example.com;
    ...
    location ^~ /en/ {
        include proxy_params;
        # 传递关键头信息,让后端WordPress识别正确的请求环境
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        proxy_pass https://en.example.com/;
        
        # 核心修复:自动替换后端返回的重定向URL
        proxy_redirect https://en.example.com/ https://example.com/en/;
    }
    ...
}

proxy_redirect这条指令是解决重定向丢失前缀的关键——它会拦截后端返回的Location头,把en.example.com/替换成example.com/en/,确保重定向路径正确。

2. Adjust WordPress Configuration in docker-compose.yml

你的WP_SITEURL和WP_HOME设置是对的,但RELOCATE=true会让WordPress自动检测站点地址,反而干扰子路径配置,同时需要添加cookie路径相关定义避免登录态问题:

environment:
  - WORDPRESS_DB_HOST=db:3306
  - WORDPRESS_DB_USER=$MYSQL_USER
  - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
  - WORDPRESS_DB_NAME=enwordpress
  - WORDPRESS_CONFIG_EXTRA= define('WP_SITEURL','https://example.com/en/'); define('WP_HOME','https://example.com/en/'); define('WP_DEBUG', true ); define('RELOCATE', false ); define('FORCE_SSL_ADMIN', true ); define('ADMIN_COOKIE_PATH', '/en/'); define('COOKIEPATH', '/en/'); define('SITECOOKIEPATH', '/en/');
  • RELOCATE=false:关闭自动地址检测,强制使用我们设定的WP_SITEURL和WP_HOME
  • FORCE_SSL_ADMIN=true:确保后台强制使用HTTPS,避免混合内容问题
  • ADMIN_COOKIE_PATH/COOKIEPATH/SITECOOKIEPATH:设置cookie的作用路径为/en/,避免登录态在根路径失效

3. Ensure Backend Server Nginx Passes Correct Headers

独立服务器的Nginx需要把前端的HTTPS协议信息传递给WordPress容器,否则WordPress可能会生成HTTP的重定向地址:

server {
    server_name en.example.com;
    location / {
        include proxy_params;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        proxy_pass http://127.0.0.1:4080; # server port 4080 is mapped to container port 80
    }
}

4. Verify Apache & .htaccess Configuration

你的.htaccess配置看起来已经正确设置了RewriteBase /en/和对应的规则,只需确保容器内的Apache把DocumentRoot设为/var/www/html/en——你已经通过working_dir设置了容器的工作目录,这部分应该没问题,但如果还是有问题,可以在容器内修改Apache的虚拟主机配置文件(通常在/etc/apache2/sites-available/000-default.conf),把DocumentRoot改为/var/www/html/en。

Final Steps

  1. 重启主服务器的Nginx服务:sudo systemctl restart nginx
  2. 重启独立服务器的Nginx服务:sudo systemctl restart nginx
  3. 重启WordPress容器:docker-compose down && docker-compose up -d

完成这些操作后,访问example.com/en/wp-admin/就不会再被重定向到example.com/wp-admin/了,后台的所有链接也会正确携带/en/前缀。

内容的提问来源于stack exchange,提问作者Hossein Mohammadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:02:33