这里用的是集成开发环境XAMPP,假设已经配置好ssl证书,不知如何申请ssl证书者请自行百度。修改Apache相关配置文件,强制所有http跳转到https,假设网站域名为xxx.com。
1、在httpd-ssl.conf中 <VirtualHost _default_:443>下面增加设置:
DocumentRoot "D:/web/xxx"
ServerName xxx.com:443
ServerAdmin admin@example.com
ErrorLog "D:/xampp/apache/logs/xxx_https_error.log"
TransferLog "D:/xampp/apache/logs/xxx_https_access.log"
2、在httpd-vhosts.conf文件的虚拟域名配置增加以下内容:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
这样,当输入 www.xxx.com 访问网站时,会自动跳转到 https://www.xxx.com
广州VI设计公司https://www.houdianzi.com
完整的<VirtualHost>记录为:
<VirtualHost *:80>
<Directory "D:/web/xxx">
Options FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
ServerAdmin 877040117@qq.com
DocumentRoot "D:/web/xxx"
ServerName www.xxx.com
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
ErrorLog "logs/xxx-error.log"
CustomLog "logs/xxx-access.log" common
</VirtualHost>