简体   繁体   English

通过 wp-config.php 在 wordpress 上正确强制 SSL

[英]Correctly force SSL on wordpress via wp-config.php

If I edit the wp-config.php I am supposed to add:如果我编辑wp-config.php我应该添加:

define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);

However, my website has .htaccess rules to force https and www across the entire website:但是,我的网站有.htaccess规则,可以在整个网站上强制使用 https 和 www:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^website.com
RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301]

I know there are other rewriterules available, but again not sure which one is correct.我知道还有其他重写器可用,但又不确定哪个是正确的。

Which of the following 3 should I be using in wp-config.php我应该在wp-config.php使用以下哪三个

1 - Without isset(), with curly brackets, with server_port 1 - 不带 isset(),带大括号,带 server_port

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

2 - Without curly brackets & without server_port? 2 - 没有大括号和没有 server_port?

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

3 - Are curly brackets needed/better or "more correct" & is server_port required? 3 - 需要大括号/更好还是“更正确”并且需要 server_port 吗?

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

I've found a few other slightly different variations of this all over the internet regarding wordpress SSL but I can't figure out what one is the correct/main one...我在互联网上发现了一些其他略有不同的关于 wordpress SSL 的变体,但我无法弄清楚哪个是正确的/主要的……

PHP code doesn't have to deal with SSL at all in such case.在这种情况下,PHP 代码根本不必处理 SSL。 Here applies classical SoC principle: if you code doesn't explicitly work with connection (in WP it does not), you should leave protocol checking to web server.这里应用了经典的SoC原则:如果您的代码没有明确地使用连接(在 WP 中它没有),您应该将协议检查留给 Web 服务器。

You should also avoid defining port in your rewrite rules.您还应该避免在重写规则中定义端口。 In case you're not using multisite WP setup, you could try:如果您不使用多站点 WP 设置,您可以尝试:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

i used this one.我用过这个。 which is fine to go on.可以继续。

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
   $_SERVER['HTTPS']='on';

if your server port is differed from 443. you can specify it .如果您的服务器端口与 443 不同,您可以指定它。 Otherwise, no need to use it once again .否则,无需再次使用它。

Corrected .htaccess rules ( as detailed on wiki.apache.org ):更正 .htaccess 规则(详见 wiki.apache.org ):

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

Normally, your code examples (1,2,3) are not necessary with Wordpress, but it looks like you have some kind of proxy based on the question.通常,Wordpress 不需要您的代码示例 (1,2,3),但看起来您有某种基于问题的代理。

  1. Not Good Will generate a PHP warning (standard php configuration) if HTTP_X_FORWARDED_PROTO is not set by the web server.不好如果 Web 服务器未设置HTTP_X_FORWARDED_PROTO生成 PHP 警告(标准 php 配置)。
  2. Good Checks variable exists before checking the value.在检查值之前, Good Checks 变量存在。 Generates no warnings.不产生警告。
  3. Good Best**最好**

** As a general rule changing _SERVER variables (like SERVER_PORT and HTTPS ) are discouraged unless you have a not-so-common setup (ie. behind proxy - which is the only reason for any of this code). ** 作为一般规则,不鼓励更改_SERVER变量(如SERVER_PORTHTTPS ),除非您有一个不太常见的设置(即在代理后面 - 这是任何此代码的唯一原因)。

For this to work for me, I had to comment out the if statement lines surrounding the line $_SERVER['HTTPS']='on';为此,我必须注释掉$_SERVER['HTTPS']='on';行周围的 if 语句行$_SERVER['HTTPS']='on';

I am using Zevenet CE 5.9, which does not provide the options for x-forwarded-for or x-forwarded-proto, hence to put it behind the reverse proxy we just force https on this way :).我正在使用 Zevenet CE 5.9,它不提供 x-forwarded-for 或 x-forwarded-proto 的选项,因此为了将它放在反向代理后面,我们只是以这种方式强制 https :)。

If you are using docker and to avoid manual configurations (by humans) this worked for me:如果您使用 docker 并避免手动配置(由人工),这对我有用:

if ( getenv('ENABLE_HTTPS')  === "true" ) {
  define( 'FORCE_SSL_ADMIN', true );
  $_SERVER['HTTPS']='on';
}

And I just need to pass a new variable ENABLE_HTTPS我只需要传递一个新变量ENABLE_HTTPS

docker run -d --name wordpress -it --rm -p 80:80 \
-e DB_HOST=10.10.10.10:3306 \
-e DB_USER=root \
-e DB_PASSWORD=secret \
-e DB_NAME=wordpress \
-e AUTH_KEY=$RANDOM_KEY \
-e SECURE_AUTH_KEY=$RANDOM_KEY \
-e NONCE_KEY=$RANDOM_KEY \
-e LOGGED_IN_KEY=$RANDOM_KEY \
-e AUTH_SALT=$RANDOM_KEY \
-e SECURE_AUTH_SALT=$RANDOM_KEY \
-e LOGGED_IN_SALT=$RANDOM_KEY \
-e NONCE_SALT=$RANDOM_KEY \
-e WP_DEBUG=true \
-e DISABLE_WP_CRON=true \
-e ENABLE_HTTPS=true wordpress:5.7.2

use this code in functions.php在functions.php中使用此代码

   add_action('template_redirect', 'f_force_ssl');
function f_force_ssl(){
    if (!is_ssl()) {
        wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
        exit();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM