将所有页面的HTTP重定向到HTTPS

本文关键字:HTTP 重定向 HTTPS | 更新日期: 2023-09-27 18:08:22

如果我输入example.com,它会像我想的那样重定向到https://example.com。但是,当输入http://example.com/blog时,它仍然重定向到https://example.com

<rewrite>
      <rules>
        <clear/>
         <rule name="Force HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>

我怎么能使它添加https到任何用户写的?

将所有页面的HTTP重定向到HTTPS

可以使用System.Web.Mvc属性。RequireHttps]用于控制器。

如果你需要在所有的控制器上,你可以使用下面的代码(只有Asp。净MVC6/核心)。

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc(options =>
    {
        options.Filters.Add(typeof(RequireHttpsInProductionAttribute));
    });
}

注意:您可以使用HSTS来避免客户端下次使用http请求

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Strict-Transport-Security" value="max-age=31536000"/>
      </customHeaders>
    </httpProtocol>
</system.webServer>