今天有一個客戶要求網(wǎng)站強制使用https訪問,因為谷歌瀏覽器如果打開http的網(wǎng)站會提示訪問不安全,因此要求強制使用https訪問,如果用戶打開的是http就強制跳轉(zhuǎn)到https上,百度上找了圈,很多種方法,但是都很麻煩,*用了一種最暴力最簡便的方法,直接修改web.config就行了,下面介紹方法:
http強制轉(zhuǎn)向https解決辦法:下面介紹一種簡單暴力的解決辦法
注意:你的iis必須安裝url重寫這個功能,如果“url重寫”沒有,需要先安裝
1.打開前端部署的文件夾
2.創(chuàng)建一個文件,命名為 web.config
3.編輯web.config,內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="httptohttps" 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>
下面這種是設(shè)置了默認文件名的方法:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.html" />
<add value="index.asp" />
<add value="index.htm" />
<add value="default.html" />
<add value="default.htm" />
<add value="default.asp" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="httptohttps" 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>
上面兩種方法你挑一種即可。
案例參考:
http://www.henrymachinery.com/
http://www.brickmachine.ltd/
http://www.legobrickmachine.com/
這三個網(wǎng)站你測試一下,打開上面網(wǎng)站域名,自動跳轉(zhuǎn)到https了
在網(wǎng)站上的方法:https://blog.csdn.net/xuliqi666/article/details/128129015