一、前言
前文我们讲了 树莓派/Debian Apache2 配置自建 CA 实现 HTTPS(SSL) 服务 ,实现了通过 SSL 建立安全连接,但依然可以通过 HTTP 访问。本文讲述如何将 Raspberry/Debian HTTP 默认的 80 端口,重定向到 HTTPS 的 443 端口。
二、修改 HTTP 配置文件 000-default.conf
1. 开始编辑 000-default.conf
sudo vim /etc/apache2/sites-available/000-default.conf
2. 添加语句
在任意一处添加语句:
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
3. :wq 保存退出
三、修改 HTTPS 配置文件 default-ssl.conf
1. 开始编辑 000-default.conf
sudo vim /etc/apache2/sites-available/default-ssl.conf
2. 修改语句
修改 ServerName 后面的字符串为 域名 。
ServerName example.com
-
如果没有申请域名,可替换为 IP 地址,例:
ServerName 192.168.0.130
2. 添加语句
在 ServerName 语句后添加语句:
ServerAlias www.example.com
Protocols h2 http:/1.1
使用上述配置,我们将强制使用 HTTPS 。
四、 重新加载Apache服务以使更改生效
sudo systemctl reload apache2
sudo /etc/init.d/apache2 restart
五、成功测试
完成上述步骤且无错误后,打开浏览器输入:
http://<域名或IP>
就会跳转到:
https://<域名或IP>
六、HTTPS 实现 www 重定向到 非www 网页
请参考博客 HTTPS 实现 www 重定向到 非www 网页 。
来源:https://www.cnblogs.com/Yogile/p/12493961.html |