我有这个应用程序,我刚刚为其安装了
SSL
证书。是的,我试图重定向用户只使用HTTPS://url.com
。并阻止他们使用http://url.com site
。但是,由于我缺乏对定义模式和条件的正则表达式的理解,不幸的是,我找不到包含如何定义这些规则的一些示例的指南。我想要一个具体的例子来说明如何设置它https://{HTTP_HOST}{REQUEST_URI}
.
确保您添加了 URL 重写功能。在 IIS 管理器中,在 URL 重写部分配置以下内容。
Matches the pattern
Wildcards
*
条件
{HTTPS}
Matches the pattern
off
行动
Redirect
https://{HTTP_HOST}{REQUEST_URI}
Checked
Found (302)
完成后。创造条件...
{QUERY_STRING}
Matches the Pattern
off
基本上它应该是这样的:
所有使用 http 请求的流量将自动重定向到 https 端口。
我想我已经找到了无需重写 url 的解决方案。在 IIS 中,右键单击网站,选择“管理网站 - 高级设置”,展开“行为”,展开“HSTS”并将属性“Enabled”和“RedirectHttpToHttps”设置为“True”。
更新: 正如@jonasfh 指出的那样,您无论如何都需要绑定到 http 和 https,因为只有对 http 的成功请求才会重定向到 https。因此,如果对 http 的请求不成功(因为不存在 http 绑定),则不会重定向到 https。谢谢。
Update2:只是想在设置后添加一个站点需要从 IIS 重新启动。
来自 Global.asax 的方法
protected void Application_BeginRequest()
{
#if !DEBUG
if (!Context.Request.IsSecureConnection)
{
if (Context.Request.Url.ToString().Contains(".well-known")) return;
Response.StatusCode = 301;
Response.RedirectPermanent(Context.Request.Url.ToString().Replace("http:", "https:"));
}
#endif
}
这就是我遵循的。
转到发布文件夹并打开 Web.config 文件并将以下重写规则粘贴到
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>