将 WordPress 切换到 https 后重定向过多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29478863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:04:37  来源:igfitidea点击:

Too many Redirects after switching WordPress to https

wordpress.htaccess

提问by mm1975

with my WordPress-Blog, I switch to https. Therefore, I have add the following code to my .htaccess-File. My Problem now is, that I get the issue "Too many Redirects". Thank you for your tips!

使用我的 WordPress 博客,我切换到 https。因此,我将以下代码添加到我的 .htaccess 文件中。我现在的问题是,我遇到了“重定向过多”的问题。谢谢你的提示!

Domain is a Subdomain:

域是子域:

https://en.example.com

https://en.example.com

# Begin Force SSL
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/ [R=301,L]
# End Force SSL
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

采纳答案by eat-sleep-code

Try changing your first three lines to:

尝试将前三行更改为:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R=301,L]

I believe, that unless you attempted to go to https://en.example.com/:443then {SERVER_PORT} will never return 443.

相信,除非您尝试访问https://en.example.com/:443,否则{SERVER_PORT} 将永远不会返回 443。

回答by abdallah Nofal

Make sure that you define the WP_SITEURL and WP_HOME at wp-config.php

确保在 wp-config.php 中定义了 WP_SITEURL 和 WP_HOME

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

And add this condition to check if the https at wp-config.php

并添加此条件以检查 wp-config.php 中的 https

    if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
        $_SERVER['HTTPS'] = 'on';
    }

回答by Ahmed

I have soved the issue using the below code inside wp-config.php

我已经使用 wp-config.php 中的以下代码解决了这个问题

define('WP_HOME','https://mywebsite.com');
define('WP_SITEURL','https://mywebsite.com');
$_SERVER['HTTPS'] = 'on';

回答by Mohammad Aboali

I had same problem nginx and had to do following in .htaccess

我在 nginx 上遇到了同样的问题,不得不在 .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R=301,L]

in wp-config.php

wp-config.php

define('FORCE_SSL_ADMIN', true);
define('RELOCATE', TRUE);
$_SERVER['HTTPS'] = 'on';
define('WP_HOME', 'https://yourdomain.com');
define('WP_SITEURL', 'https://yourdomain.com');

don't use the below with nginx unless you make sure it passes HTTPS is on to fast cgi, otherwise it won't work,

不要在 nginx 中使用下面的内容,除非你确保它通过 HTTPS 进入快速 cgi,否则它将无法工作,

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
    $_SERVER['HTTPS'] = 'on';
}

it might be of benefit log debug level in error log of nginx, this is how you get to see the flow.

它可能对 nginx 的错误日志中的日志调试级别有益,这是您查看流程的方式。

回答by Sudharshan R

Make sure that you define the WP_SITEURLand WP_HOMEat wp-config.php

确保您定义了WP_SITEURLWP_HOMEwp-config.php

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

And add this condition to check if the https at wp-config.php

并添加此条件以检查 wp-config.php 中的 https

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
    $_SERVER['HTTPS'] = 'on';
}

This works well

这很好用

回答by Chandra Kumar

Excerpt: Adding the following lines of code at the start of wp-config.phpfile resolved the redirect conflict.

摘录:在wp-config.php文件的开头添加以下代码行解决了重定向冲突。

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
   $_SERVER['HTTPS']='on';

回答by Matt Wills

Not directly the answer, but something else to note is that some browsers will cache redirects, so you might be changing your htaccess or wp-config without seeing any changes.

不是直接的答案,但要注意的是,某些浏览器会缓存重定向,因此您可能会更改 htaccess 或 wp-config 而没有看到任何更改。

Best way to debug this is using incognito mode.

调试此问题的最佳方法是使用隐身模式。

The above answers worked for me, but it took ages to work out that they had actually worked because of the browser redirect caching!

上面的答案对我有用,但是由于浏览器重定向缓存,他们花了很长时间才发现它们确实有效!