将WordPress站点从http切换至https

声明:本文全部内容为原创内容,禁止在未经授权的情况下进行任何二次创作和修改,转载请注明出处。

摘要

这篇文章将会记录如何将Wordpress站点从http切换至https。
注意:确定站点能够通过http协议正确访问,否则在经过如下步骤切换至https后,可能无法再次访问站点。

步骤一:打开wp-config.php文件

打开wp-config.php文件,假设Wordpress安装的根目录为“wordpress”:

# 如果操作目录需要root权限,则在命令开始处添加sudo
vim /wordpress/wp-config.php

步骤二:编辑文件

在文件中的第一个 <?php 后添加如下语句:

$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

步骤三:修改网站地址

在Wordpress管理后台的Settings/General选项卡中,将网址更改为正确的https网址:
Alt

步骤四:配置Apache虚拟主机文件

为wordpress服务新建一个apache配置文件,假设apache被安装在 /etc/httpd 目录,文件名为 blog.conf:

sudo vim /etc/httpd/conf.d/blog.conf
SSLPassPhraseDialog  builtin
SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout  300
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW:!RC4:

<VirtualHost _default_:80>
    # 此处替换为正确的网址
    Servername www.example.com
    ErrorLog logs/error_log
    TransferLog logs/access_log
    LogLevel warn

    # 301 跳转
    RewriteEngine on
    RewriteCond   %{HTTPS} !=on
    RewriteRule   ^(.*)  https://%{SERVER_NAME}$1 [L,R] 
</VirtualHost>

<VirtualHost _default_:443>
    # 此处替换为正确的网址
    Servername www.example.com
    ErrorLog logs/ssl_error_log
    TransferLog logs/ssl_access_log
    LogLevel warn
    SSLEngine on
    # 此处替换为SSL证书所在的目录
    SSLCertificateFile path_of_cert_file
    SSLCertificateKeyFile path_of_cert_key_file
    RewriteEngine On

    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    ProxyPass / http://www.example.com/
    ProxyPassReverse / http://www.example.com/
</VirtualHost>

对于80端口的监听(http协议),使用301跳转以强制进行https访问。

步骤五:重新加载Apache服务并访问Wordpress站点的https网址

重新加载apache服务:

sudo systemctl reload httpd

使用https网址访问Wordpress站点即可。


个人博客主站(最新内容):https://blog.davcloud.top

CSDN:不向光的红外线

知乎:不向光的红外线

微信公众号:davcloud

猜你喜欢

转载自blog.csdn.net/david394/article/details/104452726
今日推荐