• I am serving WordPress via AWS Cloudfront and my DNS records look like this,
    https://www.mysite.com -> cloudfront.net -> origin wp server.

    My issue is this:
    Wordpress is 301 permanent redirecting to the origin via the Host header passed by Cloudfront. e.g. https://www.mysite.com is redirecting to origin.mysite.com. I have eliminated Cloudfront as the culprit by doing a curl directly to the web server.

    You can test your installation via this curl.
    curl -v -H “Host: test.com” origin.yoursite.com/index.php 2>&1 | grep “Location\|HTTP”
    Response:
    > GET /index.php HTTP/1.1
    < HTTP/1.1 301 Moved Permanently
    < Location: https://test.com/

    I have checked all .htaccess files and there is no {http_host} or [R=301] directives in them. Disabled all plugins etc. Forcing a 301 redirect to www in .htaccess causes a redirect loop so thats no good. If I comment out the following lines in wp-includes/canonical.php the redirect stops and I get a response of 200. Success!

    if ( $do_redirect ) {
    
            // protect against chained redirects
    		if ( !redirect_canonical($redirect_url, false) ) {
    			wp_redirect($redirect_url, 301);
    			exit();
    		} else {
    			// Debug
    			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
    			return false;
    		}
    	} else {
    		return $redirect_url;
    	}

    But I’m not sure of the implications by doing this. Either way I believe this to be a bug as this initial Host Header 301 redirection is undesirable.

    Any idea of how to fix this or advise is appreciated.

    Thanks,
    -J

  • The topic ‘Host Header 301 redirect issue’ is closed to new replies.