當前位置:生活全書館 >

IT科技

> http怎麼做自動跳轉https (各種版本)

http怎麼做自動跳轉https (各種版本)

APache、Nginx、IIS、Tomcat等各種服務器版本設定http自動跳轉到https的方法大全

材料/工具

已部署好HTTPS證書的服務器

APache 版本

如果需要整站跳轉,則在網站的配置檔案的<Directory>標籤內,鍵入以下內容:

RewriteEngine onRewriteCond %{SERVER_PORT} !^443$RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 

(各種版本) http怎麼做自動跳轉https

如果對某個目錄做https強制跳轉,則複製以下代碼:
RewriteEngine onRewriteBase /yourfolderRewriteCond %{SERVER_PORT} !^443$#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}

如果只需要對某個網頁進行https跳轉,可以使用redirect 301來做跳轉!redirect 301/你的網頁 https://你的主機+網頁

(各種版本) http怎麼做自動跳轉https 第2張

Nginx版本

在配置80端口的檔案裏面,寫入以下內容即可。
server {listen 80;server_namelocalhost; rewrite ^(.*)$ https://$host$1 permanent;

location / {root html;indexindex.html index.htm;}

(各種版本) http怎麼做自動跳轉https 第3張

IIS 版本

使用url重定向實現全站跳轉。在此之前,請檢查網站根目錄是否有web.config檔案,如有,請先備份這裏的web.config檔案,因爲以下的配置可能會和web.config裏面跳轉衝突。
1選擇需要實現跳轉功能的網站,雙擊“URL重寫”,選擇如下圖“添加規則”。
2在彈出的對話框選擇空白規則,點擊確定。
3根據以下截圖配置新的規則,紅色框框爲需要配置或注意的選項。
4展開條件選項,點擊添加按鈕,添加如下圖條件,然後點擊確定。
5再次按下圖提示,添加條件,點擊確定。
6選擇執行操作類型,如下圖。
7填寫完畢,點擊右上角應用,應用此規則。
8最後確定完成所有設定,實際上上面的檔案是改變了網站根目錄web.config的配置檔案內容。

(各種版本) http怎麼做自動跳轉https 第4張

以上配置檔案內容如下,可以比對
<rule name="Redirect to https"stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTPS_HOST}" pattern="^(localhost)"negate="true" />
</conditions>
<action type="Redirect"url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>

(各種版本) http怎麼做自動跳轉https 第5張

TOMCAT 版本

在conf目錄下的server.xml檔案中找到以下配置,修改redirectPort參數值爲"443",默認是“8443”.
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" />

(各種版本) http怎麼做自動跳轉https 第6張

在conf目錄下的web.xml檔案內容<web-app>……</web-app>中增加以下配置
<web-app>.........<security-constraint><web-resource-collection > <web-resource-name >SSL</web-resource-name><url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint><transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint></security-constraint></web-app>

(各種版本) http怎麼做自動跳轉https 第7張

單獨頁面通用代碼段

以下方法較適合指定某一個子頁單獨https在需要強制爲https的頁面上加入以下代碼進行處理http-->https
<script type="text/javascript">var url = window.location.href;if (url.indexOf("https") < 0) {url = url.replace("http:", "https:");window.location.replace(url);}</script>

(各種版本) http怎麼做自動跳轉https 第8張

在需要強制爲http的頁面上加入以下代碼進行處理https-->http
<script language="JavaScript" type="text/JavaScript">function redirect(){var loc = location.href.split(':');if(loc=='https'){location.href='http:'+loc;}}onload=redirect</script>

(各種版本) http怎麼做自動跳轉https 第9張

PHP頁面跳轉

添加在網站php頁面內
if ($_SERVER["HTTPS"] <> "on") { $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; header("Location: ".$xredir); }

(各種版本) http怎麼做自動跳轉https 第10張
標籤: https http 跳轉 自動
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zh-hant/dianzi/1pmev2.html