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 |1 Answer
Reset to default 1So 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
版权声明:本文标题:amazon - How to prevent redirection from non-www to www or vice versa? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741317254a2371989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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