現在很多網站為了安全,開啟了 SSL 連接,那么開啟 SSL 連接之后,如何將對應的 http 訪問自動跳轉到 https 上呢?之前介紹了 IIS 用 web.config 做域名的301跳轉的方法,同樣使用 IIS 可以用 web.config 實現 http 網址自動301跳轉到 https 網址。
上一篇文章是利用訪問域名的方式進行301跳轉,也就是判斷訪客的域名,然后進行跳轉。可是 http 和 https 訪問的網址是一樣的,這樣上面這篇文章的 web.config 代碼就不能使用了。
其實換個思路就清楚了,那么判斷域名不行了,我們是不是可以直接判斷 https 狀態呢?非 https 狀態自動跳轉到 https 對應網址。
web.config 代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="HTTPS iCoA" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTPS}" pattern="^off" /> </conditions> <action type="Redirect" url="http://www.yuanzhumuban.cc/{R:0}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration>