Apache http自動跳轉到https的幾種方法

來源:互聯網
上載者:User
當你的網站使用了HTTPS之後,你可能會想把所有的HTTP請求(即連接埠80的請求),全部都重新導向至HTTPS。這時候你可以用以下的方式來做到:

在啟用了 https 之後,還要保證之前的 http 連接埠可以開啟,http 的 80 連接埠是有兩個網址的,所以這就導致需要把原來的帶 wwww 和不帶 www 的網域名稱同時指定一個 https 網址上面,需

要做兩個 Apache 的301重新導向,這個其實是很簡單的,夏日部落格的做法是直接在 .htaccess 檔案中添加兩個 301 即可,如下所示:

rewritecond %{http_host} ^www.php.cn [nc]  RewriteRule ^(.*)?$ https://www.php.cn/$1 [R=301,L]  RewriteCond %{SERVER_PORT} !^443$  RewriteRule ^(.*)?$ https://www.php.cn/$1 [R=301,L]


第一個 301 很自然就是帶 www 的跳轉到新的 https 上面了,而下面的301重新導向則是判斷如果連接埠不是80的話,則進行重新導向,這樣的話,帶www和不帶www的網域名稱就一起跳轉到 https 一個網址上面了,當然這種全站做301的方法是比較暴力的,通常情況下我們只要把主網域名稱做個301就可以了,我這裡是因為啟用了原來的兩個網域名稱。

topic.alibabacloud.com還手機了一些其它的 Apache http 跳轉到 https 的方法,僅供參考:

方法1

RewriteEngine On  RewriteBase /  RewriteCond %{SERVER_PORT} 80  RewriteRule ^(.*)$ https://www.php.cn/$1 [R=301,L]

方法二

RewriteEngine on  RewriteCond %{SERVER_PORT} !^443$  RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]

#整站跳轉

方法三

RewriteEngine on  RewriteBase /yourfolder  RewriteCond %{SERVER_PORT} !^443$  #RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]  RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

#以上至針對某個目錄跳轉, yourfolder就是目錄名

方法4

redirect 301  /你的網頁 https://你的主機+網頁

#至針對某個網頁跳轉

方法5

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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.