Apache環境下強制跳轉到https
阿新 • • 發佈:2019-08-01
這裡用的是整合開發環境XAMPP,假設已經配置好ssl證書,不知如何申請ssl證書者請自行百度。
修改Apache相關配置檔案,強制所有http跳轉到https,假設網站域名為xxx.com。
1、在httpd-ssl.conf中 <VirtualHost _default_:443>下面增加設定:
DocumentRoot "D:/web/xxx" ServerName xxx.com:443 ServerAdmin [email protected] 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
完整的<VirtualHost>記錄為:
<VirtualHost *:80>
<Directory "D:/web/xxx">
Options FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
ServerAdmin [email protected]
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>