這篇文章給大家介紹IIS與APACHE實現(xiàn)HTTP重定向到HTTPS,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="redirect to HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
Apache http跳轉(zhuǎn)https配置
修改.htaccess文件,在文件里增加如下幾行:
RewriteEngine On RewriteBase / RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
另一種寫法是:
RewriteEngine on RewriteBase / RewriteCond %{SERVER_PORT} !^443$ RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
nginx配置
nginx的rewrite方法
思路
這應(yīng)該是大家最容易想到的方法,將所有的http請求通過rewrite重寫到https上即可
配置
server { listen 192.168.1.111:80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent; }
搭建此虛擬主機完成后,就可以將http://test.com的請求全部重寫到https://test.com上了
nginx的497狀態(tài)碼
error code 497
497 - normal request was sent to HTTPS
解釋:當此虛擬站點只允許https訪問時,當用http訪問時nginx會報出497錯誤碼
思路
利用error_page命令將497狀態(tài)碼的鏈接重定向到https://test.com這個域名上
配置
server {
listen 192.168.1.11:443; #ssl端口
listen 192.168.1.11:80; #用戶習慣用http訪問,加上80,后面通過497狀態(tài)碼讓它自動跳到443端口
server_name test.com;
#為一個server{......}開啟ssl支持
ssl on;
#指定PEM格式的證書文件
ssl_certificate /etc/nginx/test.pem;
#指定PEM格式的私鑰文件
ssl_certificate_key /etc/nginx/test.key;
#讓http請求重定向到https請求
error_page 497 https://$host$uri?$args;
}
index.html刷新網(wǎng)頁
思路
上述兩種方法均會耗費服務(wù)器的資源,我們用curl訪問baidu.com試一下,看百度的公司是如何實現(xiàn)baidu.com向www.baidu.com的跳轉(zhuǎn)
可以看到百度很巧妙的利用meta的刷新作用,將baidu.com跳轉(zhuǎn)到www.baidu.com.因此我們可以基于http://test.com的虛擬主機路徑下也寫一個index.html,內(nèi)容就是http向https的跳轉(zhuǎn)
index.html
<html>
<meta http-equiv="refresh" content="0;url=https://test.com/">
</html>
nginx虛擬主機配置
server {
listen 192.168.1.11:80;
server_name test.com;
location / {
#index.html放在虛擬主機監(jiān)聽的根目錄下
root /srv/www/http.test.com/;
}
#將404的頁面重定向到https的首頁
error_page 404 https://test.com/;
}
關(guān)于IIS與APACHE實現(xiàn)HTTP重定向到HTTPS就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:IIS與APACHE實現(xiàn)HTTP重定向到HTTPS-創(chuàng)新互聯(lián)
本文URL:http://jinyejixie.com/article38/jsgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、搜索引擎優(yōu)化、企業(yè)建站、網(wǎng)站內(nèi)鏈、App設(shè)計、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容