如何設置http自動跳轉到https?
apache環境下,配置好https后,需要設置url重定向規則,使網站頁面的http訪問都自動轉到https訪問。
<Directory “C:/www”>
…
</Directory>
修改其中的 AllowOverride None 為 AllowOverride All
2)編輯器打開.htaccess文件,寫入如下規則:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/tz.php
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
解釋:
%{SERVER_PORT} —— 訪問端口
%{REQUEST_URI} —— 比如如果url是 http://localhost/tz.php,則是指 /tz.php
%{SERVER_NAME} —— 比如如果url是 http://localhost/tz.php,則是指 localhost
以上規則的意思是,如果訪問的url的端口不是443,且訪問頁面不是tz.php,則應用RewriteRule這條規則。這樣便實現了:訪問了 http://localhost/index.php 或者 http://localhost/admin/index.php 等頁面的時候會自動跳轉到 https://localhost/index.php 或者 https://localhost/admin/index.php,但是訪問 http://localhost/tz.php 的時候就不會做任何跳轉,也就是說 http://localhost/tz.php 和 https://localhost/tz.php 兩個地址都可以訪問。
