让 HTTP 自动跳转到 HTTPS 的 web.config 配置

1、下载安装URL重写模块:Microsoft URL Rewrite Module

2、IIS 10 安装 URLRewrite 组件方式:

  • Open Regedit > HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp
  • Edit "MajorVersion" and set the "DECIMAL" value to 9
  • Hit F5 while in Regedit
  • Now go install the ReWrite 2.0 module.
  • Change the "MajorVersion" back to "DECIMAL" value of 10
  • Hit F5 while in Regedit
  • Close Regedit

3、SSL设置,取消勾选“要求SSL”(很重要)

图-01

4、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="Found" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>

5、设置301重定向

要实现对主域名访问(即:domain.com)自动跳转到带www的二级域名(即:www.domain.com),则可以新建一个空目录网站,并绑定不带www的主域名之下。最重要的是对这个绑定了主域名的空目录网站做301永久重定向。如图:

图-02

也可以通过"web.config"文件直接配置HTTP重定向。

图-03

作者: Hugh

Welcome to Wan's world~