1:申请SSL证书


2:在Typecho中设置(非必须?)
登录后台 -> 设置 -> 基本设置 -> 站点地址改成HTTPS的域名。
在根目录的config.inc.php文件中,插入一行:

define('__TYPECHO_SECURE__',true);

由于Chrome浏览器对HTTPS要求较高,Firefox已经显示小绿锁,可是Chrome还是有警告提示,F12查看,评论表单的action地址还是http,找到站点主题目录下的 comments.php 文件,并搜索 $this->commentUrl(),将其替换为:echo str_replace("http","https",$this->commentUrl()); 最后保存。


3:修改.haccess文件实现重定向
(1) Redirect All Web Traffic
If you have existing code in your .htaccess, add the following:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

(2) Redirect Only a Specific Domain
For redirecting a specific domain to use HTTPS, add the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

(3) Redirect Only a Specific Folder
Redirecting to HTTPS on a specific folder, add the following:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]

Note: Replace “yourdomain” with your actual domain name wherever required. Also, in case of the folder, replace /folder with the actual folder name.


4:清除浏览器缓存并访问网站,浏览器应该已显示安全的小绿锁标识,如果没有,可通过浏览器F12分析是否还加载了不安全的HTTP资源。

参考:How to redirect HTTP to HTTPS Using .htaccess

最后修改:2019 年 08 月 23 日
如果觉得我的文章对你有用,欢迎赞赏。