如何防止从非WWW重定向到WWW或从WWW重定向到WWW?

时间:2021-09-15 作者:alancc

在WordPress中,如果我定义:

define(\'WP_SITEURL\', \'https://www.example.com\');
define(\'WP_HOME\', \'https://www.example.com\');
然后是所有非www版本,如https://example.com 将重定向到www版本,响应标头将为:

X-Redirect-By: WordPress
对于一般情况,这是可以的。但是,现在我想阻止这样的重定向,因为我使用的是Amazon Cloutfront,应该停止重定向,否则会出现错误。看见https://stackoverflow.com/questions/20503322/cloudfront-distribution-with-custom-origin-redirects-request/22571467#22571467

那么如何防止重定向呢?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

那么如何防止重定向呢?

在WordPress站点的前端/公共端,重定向由redirect_canonical() 它被钩住了template_redirect, 如果你真的要禁用它non-www to www or www to non-www 重定向,您可以尝试以下操作(将代码添加到主题的functions.php 文件):

用于禁用non-www to www 重定向:

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;
}
www to non-www 重定向:

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;
}
其他注意事项:正如我在评论中所说,阻止非www到www(反之亦然)重定向可能会导致CORS 源主机名不匹配导致的错误,例如。example.com != www.example.com, 所以请记住这一点。

相关推荐

使用wp-Super-cache和Amazon CloudFront-我可以直接提供文件(无CDN)吗?

我正在使用启用cdn支持的wp super cache插件(aws cloudfront)。我也在使用重新加载的wp表。后一个插件通过ajax包含一个文本文件,该文件由cdn提供服务。该插件不执行任何类型的jsonp操作,因此文件会遇到访问控制允许源问题。在我去修改插件以正确提取文件之前,有人知道我如何限制cdn存储此文件,并直接提供它吗?