我的目标是三重
- 在地址栏中使用不带 www 的域名
- 自动从 HTTP 重定向到 HTTPS,不会失败或发出问题,也无需在任何类型的浏览器中接受证书
- 简单的故障保护安全。如果谷歌能做到,我为什么能做到?
在上一篇文章中使用“烟雾”端口的想法很聪明。在使用端口 80 时,我不断收到蓝色的 IIS 欢迎页面和 http 协议。假端口似乎迫使 asp.net 网页实际读取 Web 配置中的重定向代码。我将所有的 http 绑定更改为端口 81。添加到 web.config 中的重写 url 代码如下所示。
要测试的另一件重要的事情是关闭“必需的 SSL”,因为一篇文章表明它可能与 IIS 重写 URL 冲突(任何不显示错误的冲突都可能是一个令人头疼的问题)。在关闭 IIS 的“SSL 设置”“必需”开关并仅选中“忽略”之前,来自数百个帖子或演练设置的重写 URL 的任何组合似乎都失败了(例如,这特定于 IIS10 Win 2016,但可能与以前的所有 IIS 相同)。
如果从静态 IP 托管,在测试期间确保请求在 LAN 之外也很重要。只需使用您的移动热点和平板电脑上的一些浏览器(例如 samsung、brave、mozilla、edge 等)来查看每个浏览器的响应。部分原因是基于 TLS1.2 的实现和所有其他协议和密码的禁用。
最后不要忘记在测试页面加载之前不断删除 cookie/历史记录。在某些情况下,Android 上的“擦除缓存分区”会删除任何可能导致设备出现问题的临时文件(尤其是在测试期间)。
这个问题可能是一场噩梦,因为本来应该有效的事情绝对行不通,即使逻辑是“防弹逻辑”。
即使考虑和测试了所有这些因素,它也很有可能无法在某些浏览器中运行......它只是拒绝从
h*tp://domain.com
IIS 欢迎页面...例如 edge、brave 等
按照流行的 ssl 站点的说明,使用 3 条规则重写 Sa 示例
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect www" stopProcessing="true">
<match url="www.domain.com"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent"/>
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="domain.com"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent"/>
</rule>
<rule name="Redirect Canonical HTTP to HTTPS" stopProcessing="true">
<match url="domain.com"/>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"/>
</rule>
</rules>
<outboundRules>
<rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
<match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*"/>
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true"/>
</conditions>
<action type="Rewrite" value="max-age=31536000; includeSubDomains; preload"/>
</rule>
</outboundRules>
</rewrite>
即使是 C# asp.net 中的一些页面加载代码也可能什么都不做……但值得一试……也许在 IIS 领域是不可能的。
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (!context.Request.IsSecureConnection)
{
UriBuilder secureUrl = new UriBuilder(context.Request.Url);
secureUrl.Scheme = "https";
secureUrl.Port = 443;
context.Response.Redirect(secureUrl.ToString(), false);
}
}