0

In WordPress, it seems that if I define:

define('WP_SITEURL', 'https://www.example.com');
define('WP_HOME', 'https://www.example.com');

Then all non-www version such as https://example.com will be redirected to www version and the response header will be:

X-Redirect-By: WordPress

This is OK for general cases. However, now I want to prevent such a redirection, because I am using Amazon Cloutfront and the redirection should be stopped, otherwise, there will be error. See https://stackoverflow.com/questions/20503322/cloudfront-distribution-with-custom-origin-redirects-request/22571467#22571467

So how to prevent the redirection?

5
  • Can't you just set the Cloudfront origin to use the www? And note that if you prevent the redirection (non-www to www), then you might end up with a CORS error due to mismatched origin hostname (example.com != www.example.com).
    – Sally CJ
    Sep 15 at 10:02
  • @SallyCJ, if set Origin to use www.example.com, while I use Cloudfront to cache everything, so I also need to modify the CName of www.example.com to point to the Cloudfont URL, that will cause a deadloop.
    – alancc
    Sep 15 at 21:05
  • "the redirection should be stopped, otherwise, there will be error" - have you actually encountered an error, or did you say that just theoretically (based on the SO post)? Have you already consulted with Amazon Cloudfront or checked their docs regarding the latest best/recommended practices?
    – Sally CJ
    Sep 16 at 0:50
  • @SallyCJ, Yes, I encounter an error indicating "too many redirects", then I search online and find the stackoverflow post.
    – alancc
    Sep 16 at 1:24
  • In that case, then I presumed you're aware of the possible drawbacks of preventing the redirection, and let me know if my answer helped?
    – Sally CJ
    Sep 16 at 7:09
1

So how to prevent the redirection?

On the front-end/public side of a WordPress site, that redirection is done by redirect_canonical() which is hooked on template_redirect, and if you really must disable that non-www to www or www to non-www redirection, you can try the following (add the code to your theme's functions.php file):

  • For disabling non-www to www redirection:

    add_action( 'parse_request', 'wpse_395638_1' );
    function wpse_395638_1( $wp ) {
        // If the current URL doesn't have any path and without the www prefix, e.g.
        // https://example.com or https://example.com/?foo=bar, then we completely
        // disable the canonical redirect by unhooking redirect_canonical().
        if ( empty( $wp->request ) ) {
            $host = parse_url( home_url(), PHP_URL_HOST );
    
            if ( 'www.' . $_SERVER['SERVER_NAME'] === $host ) {
                remove_action( 'template_redirect', 'redirect_canonical' );
            }
        }
    }
    
    // This snippet doesn't disable canonical redirect, but the snippet ensures
    // that the redirect URL doesn't use the www prefix, unless the current URL
    // uses it.
    add_filter( 'redirect_canonical', 'wpse_395638_2', 10, 2 );
    function wpse_395638_2( $redirect_url, $requested_url ) {
        $host  = parse_url( $redirect_url, PHP_URL_HOST );
        $host2 = parse_url( $requested_url, PHP_URL_HOST );
    
        // If the current URL doesn't use www, we remove it from the redirect URL.
        if ( "www.$host2" === $host ) {
            $redirect_url = preg_replace( '#^http(s?)://www.#', 'http$1://', $redirect_url );
        }
    
        return $redirect_url;
    }
    
  • For disabling www to non-www redirection:

    add_action( 'parse_request', 'wpse_395638_1' );
    function wpse_395638_1( $wp ) {
        // If the current URL doesn't have any path and with the www prefix, e.g.
        // https://www.example.com or https://www.example.com/?foo=bar, then we
        // completely disable the canonical redirect by unhooking redirect_canonical().
        if ( empty( $wp->request ) ) {
            $host = parse_url( home_url(), PHP_URL_HOST );
    
            if ( "www.$host" === $_SERVER['SERVER_NAME'] ) {
                remove_action( 'template_redirect', 'redirect_canonical' );
            }
        }
    }
    
    // This snippet doesn't disable canonical redirect, but the snippet ensures
    // that the redirect URL uses the www prefix, unless the current URL doesn't
    // use it.
    add_filter( 'redirect_canonical', 'wpse_395638_2', 10, 2 );
    function wpse_395638_2( $redirect_url, $requested_url ) {
        $host  = parse_url( $redirect_url, PHP_URL_HOST );
        $host2 = parse_url( $requested_url, PHP_URL_HOST );
    
        // If the current URL uses www, we add it back to the redirect URL.
        if ( "www.$host" === $host2 ) {
            $redirect_url = preg_replace( '#^http(s?)://#', 'http$1://www.', $redirect_url );
        }
    
        return $redirect_url;
    }
    

Additional Note: As I said in the comments, preventing the non-www to www (or vice-versa) redirection could lead to CORS errors due to mismatched origin hostname, e.g. example.com != www.example.com, so just keep that in mind.

1
  • 1
    Thank you very much for the info.
    – alancc
    Sep 16 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.