apache如何設置http自動跳轉到https


如何設置http自動跳轉到https?

apache環境下,配置好https后,需要設置url重定向規則,使網站頁面的http訪問都自動轉到https訪問。

1、先打開url重定向支持
 
  1)打開Apache/conf/httpd.conf,找到 #LoadModule rewrite_module modules/mod_rewrite.so 去掉#號。   
 
  2)找到你網站目錄的<Directory>段,比如我的網站目錄是c:/www,找到
    <Directory “C:/www”>
    …
    </Directory>
    修改其中的 AllowOverride None 為 AllowOverride All
 
  3)重啟apache服務
 
2、設置重定向規則
 
  1)在你網站目錄下放一個.htaccess文件。windows環境下,不能把文件直接改名為.htaccess,會提示你必須輸入文件名。所以我們先新建一個“新建文本文檔.txt”文檔,記事本打開,選擇另存為,保存類型選擇“所有文件(*.*)”,文件名輸入“.htaccess”,保存。這樣便生成了一個.htaccess文件。

  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 兩個地址都可以訪問。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM