admin管理员组

文章数量:1287629

In WordPress, it seems that if I define:

define('WP_SITEURL', '');
define('WP_HOME', '');

Then all non-www version such as 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

So how to prevent the redirection?

In WordPress, it seems that if I define:

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

Then all non-www version such as https://example 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/questions/20503322/cloudfront-distribution-with-custom-origin-redirects-request/22571467#22571467

So how to prevent the redirection?

Share Improve this question asked Sep 15, 2021 at 1:58 alanccalancc 416 bronze badges 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 != www.example). – Sally CJ Commented Sep 15, 2021 at 10:02
  • @SallyCJ, if set Origin to use www.example, while I use Cloudfront to cache everything, so I also need to modify the CName of www.example to point to the Cloudfont URL, that will cause a deadloop. – alancc Commented Sep 15, 2021 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 Commented Sep 16, 2021 at 0:50
  • @SallyCJ, Yes, I encounter an error indicating "too many redirects", then I search online and find the stackoverflow post. – alancc Commented Sep 16, 2021 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 Commented Sep 16, 2021 at 7:09
Add a comment  | 

1 Answer 1

Reset to default 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 or https://example/?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 or https://www.example/?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 != www.example, so just keep that in mind.

本文标签: amazonHow to prevent redirection from nonwww to www or vice versa