asp.net - 在重定向到 web.config 中的 www 子域期间在域根上添加 HSTS http header

标签 asp.net redirect iis ssl hsts

我有一个 asp.net web 应用程序,它被子域“www”上的搜索引擎索引。我真的不想改变这一点:对根域的请求都设置为永久重定向到 www 版本,这没问题。

我已经在站点上启用了 HSTS,但是由于重定向,我添加的 HSTS 出站 header 规则在对域根目录的第一次请求中从未命中。 (它适用于后续的 https 请求,因为没有重定向)。这是一个问题,因为我想提交网站以进行 HSTS 预加载——这要求重定向包含 HSTS 响应 header ....

我已经尝试将规则的 stopProcessing 属性设置为 false(希望设置 HSTS header 的出站规则即使在重定向时也会运行)但无济于事。

以下是我的配置文件的相关摘录:

<rewrite>
  <rules>
    <rule name="Canonical Host Name, HTTPS enabled" stopProcessing="false">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" negate="true" pattern="www.mysite.co.uk" />
        <add input="{HTTP_HOST}" negate="true" pattern="^[a-z0-9]+\.cloudapp\.net$" />
        <add input="{HTTP_HOST}" negate="true" pattern="localhost" />
      </conditions>
      <action type="Redirect" url="https://www.mysite.co.uk/{R:1}" redirectType="Permanent" />
    </rule>

  </rules>

        <!-- hsts | http://www.hanselman.com/blog/HowToEnableHTTPStrictTransportSecurityHSTSInIIS7.aspx -->
     <outboundRules rewriteBeforeCache="true">
            <rule name="Add Strict-Transport-Security" enabled="true">
                <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    <add input="{HTTP_HOST}" pattern="(mysite.co.uk|www.mysite.co.uk)" ignoreCase="true" />
                </conditions>
                <action type="Rewrite" value="max-age=31536000; includeSubDomains; preload" />
            </rule>  
    </outboundRules>

</rewrite>

最佳答案

必须按如下方式添加 header :

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

即使在发送重定向时也会发送 header 。 我删除了 outboundRules 部分。

关于asp.net - 在重定向到 web.config 中的 www 子域期间在域根上添加 HSTS http header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013967/

相关文章:

c# - 动态 GridView 生成

ASP.net:网站或 Web 应用程序项目

javascript - 如何创建可重复使用的 Web 控件库

windows - 一台 Windows 服务器上的 IIS 和 TOMCAT

asp.net - Cache.Insert ("") 绝对过期时间为 UTC 偏移量

python - 带有状态码 200 重定向的链接

facebook - 重定向并保留我的图标

c# - 重定向到 "Thank you"页面并立即提供下载文件的保存对话框

asp.net - 使用 ARR 的 IIS 反向代理存在目录级别问题

mysql - 更改localhost端口影响mysql