IIS配置web.config实现http 301重定向到https

最近帮朋友申请ssl升级网站到https,需要将原有的http 301重定向到https,这牵扯到将http://www.domain.com、http://domain.com以及https://domain.com统一跳转到https://www.domain.com,也就是把域名从www和不带www的http,以及https不带www的都跳转到带www的https。可以在web.config文件内的<system.webServer>节点内添加如下配置即可。

  1. <system.webServer>
  2. <rewrite>
  3. <rules>
  4. <!--http://www跳转到https://www-->
  5. <rule name="WWW HTTP to HTTPS" stopProcessing="true">
  6. <match url="(.*)" />
  7. <conditions>
  8. <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  9. <add input="{HTTP_HOST}" pattern="^www" ignoreCase="true" />
  10. </conditions>
  11. <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
  12. </rule>
  13. <!--http://跳转到https://www-->
  14. <rule name="None WWW HTTP to HTTPS" stopProcessing="true">
  15. <match url="(.*)" />
  16. <conditions>
  17. <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  18. <add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
  19. </conditions>
  20. <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
  21. </rule>
  22. <!--https://跳转到https://www-->
  23. <rule name="None WWW HTTPS to HTTPS" stopProcessing="true">
  24. <match url="(.*)" />
  25. <conditions>
  26. <add input="{HTTPS}" pattern="on" ignoreCase="true" />
  27. <add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
  28. </conditions>
  29. <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
  30. </rule>
  31. </rules>
  32. </rewrite>
  33. </system.webServer>
<system.webServer>
  <rewrite>
    <rules>
      <!--http://www跳转到https://www-->
      <rule name="WWW HTTP to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          <add input="{HTTP_HOST}" pattern="^www" ignoreCase="true" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
      </rule>
      <!--http://跳转到https://www-->
      <rule name="None WWW HTTP to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          <add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
      </rule>
      <!--https://跳转到https://www-->
      <rule name="None WWW HTTPS to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTPS}" pattern="on" ignoreCase="true" />
          <add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

另外,如果你的iis没有安装重定向模块,则需首先添加url重定向模块,否则上面的配置无法生效。

发表评论