IIS HTTP重定向到HTTPS

时间:2022-12-10 14:23:16

 

如果是ASP.NET站点,则直接在Web.config文件中添加以下配置节,作为<configuration>的子元素放在文件末尾即可。

IIS HTTP重定向到HTTPSIIS HTTP重定向到HTTPS

Web.config配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

<system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                        <add input="{HTTPS_HOST}" pattern="^(localhost)" negate="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}:443/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>