在 虚拟机 IIS 服务器上装了 SSL 后要把所有的 HTTP 链接重定向到 HTTPS 上,使用了下面的 web.config 文件内容
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
这条规则是完全没有问题的,{HTTPS} 为服务器变量,当请求 URL 为 HTTP 请求时,该值为 OFF,当为 HTTPS 时,该值为 ON,还有一个变量 SERVER_PORT_SECURE ,当 HTTP 请求时,为 0,否则为 1。该条规则匹配所有 HTTP 的请求然后进行重定向。但是问题来了,在我的网站上怎么也重定向不了,后来发现是不论何种链接,{HTTPS} 的值都为 OFF,导致重定向多次,网站开不了。后来才知道,虚拟机上已经废弃了该变量,使用 <span style="color: #ff0000;">{HTTP_FROM_HTTPS}</span> 值替代,当 HTTP 请求时, HTTP_FROM_HTTPS 为 ON,更改之,重定向成功!