![如何强制/重定向 http 到 https?](https://linux22.com/image/737341/%E5%A6%82%E4%BD%95%E5%BC%BA%E5%88%B6%2F%E9%87%8D%E5%AE%9A%E5%90%91%20http%20%E5%88%B0%20https%EF%BC%9F.png)
我已经配置了以下 VirtualHost:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/mysite.com/public_html/
ServerName www.mysite.com
ServerAlias mysite.com
<Directory "/var/www/mysite.com/public_html">
#Options FollowSymLinks
#AllowOverride All
#Order allow,deny
#Allow from all
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
我想要:
- 强制仅使用 https 连接
- 并允许在用户使用不带 www 的 mysite.com 时进行连接
怎么做?
谢谢
答案1
首先,您应该为 HTTPS 定义 443 端口虚拟主机。
其次,添加从端口 80 到 443 的重写规则。
样本片段:
<VirtualHost __default_:443>
ServerAdmin [email protected]
DocumentRoot /var/www/mysite.com/public_html/
ServerName www.example.com
ServerAlias example.com
SSLEngine On
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect permanent / https://www.example.com/
</VirtualHost>