• haroonejaz444

    (@haroonejaz444)


    I am trying to forward/proxy all the traffic from only blog links from my old worpress website that is https://old_wordpress_host.com/blog to new host https://new_wordpress_host.com/

    nginx block for proxy on old_wordpress_host.com.conf

    location ^~ /blog {
    rewrite ^/blog(/.*)$ $1 break;
    proxy_ssl_server_name on;
    proxy_pass_request_headers on;
    proxy_pass https://new_wordpress_host.com/;
    
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    server_name_in_redirect off;

    }

    And then in wordpress wp-config.php on new wordpress new_wordpress_host.com

    define('.COOKIE_DOMAIN.', 'old_wordpress_host.com');
    define('.SITECOOKIEPATH.', '.');
    
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
    $_SERVER['REMOTE_ADDR'] = $list[0];
    
        define('WP_SITEURL', 'https://old_wordpress_host.com/blog/');
        define('WP_HOME', 'https://old_wordpress_host.com/blog/');
        $_SERVER['HTTP_HOST'] = 'old_wordpress_host.com';
        $_SERVER['REMOTE_ADDR'] = 'https://old_wordpress_host.com';
        $_SERVER[ 'SERVER_ADDR' ] = 'old_wordpress_host.com';
    
        if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){
                $_SERVER['HTTPS']='on';
        }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Niko

    (@nheeko)

    Hi @haroonejaz444,

    Your configuration seems mostly correct, but I noticed a few potential issues and improvements that could be made.

    For your Nginx Configuration (old_wordpress_host.com.conf):

    location ^~ /blog {
        proxy_ssl_server_name on;
        proxy_pass_request_headers on;
        proxy_pass https://new_wordpress_host.com/;
        
        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;
    
        # Add the following line to adjust the URL before passing it to the new host
        proxy_set_header X-Forwarded-URI $uri;
    
        # Adjust the rewrite to include the blog path in the new host
        rewrite ^/blog(/.*)$ $1 break;
    }
    

    For your WordPress wp-config.php (on new_wordpress_host.com) try to use this:

    define('COOKIE_DOMAIN', 'old_wordpress_host.com');
    define('SITECOOKIEPATH', '/blog');
    
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $list = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $_SERVER['REMOTE_ADDR'] = $list[0];
    
        define('WP_SITEURL', 'https://old_wordpress_host.com/blog');
        define('WP_HOME', 'https://old_wordpress_host.com/blog');
        $_SERVER['HTTP_HOST'] = 'old_wordpress_host.com';
        $_SERVER['REMOTE_ADDR'] = 'old_wordpress_host.com';
        $_SERVER['SERVER_ADDR'] = 'old_wordpress_host.com';
    
        if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
            $_SERVER['HTTPS'] = 'on';
        }
    }
    

    Make sure that in your WordPress settings, the URL is set to https://old_wordpress_host.com/blog to reflect the proxy configuration. Additionally, clear any caching plugins or server caches after making these changes to ensure the new settings take effect.

    Always back up your configurations and test these changes in a non-production environment before implementing them on your live servers.

    Best regards,
    Niko

    Thread Starter haroonejaz444

    (@haroonejaz444)

    My case is the case A on WP Engine reverse proxy. I have implemented according to their document: wpengine.com/support/using-a-reverse-proxy-with-wp-engine But its not working I have updated according to your suggestion and it still going in loop

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP behind a Reverse Proxy – redirecting in loop’ is closed to new replies.