![](/img/trans.png)
[英]IIS 7.5 URL Rewrite - Redirect from http to https for account controller but from https to http for everything else
[英]URL Rewrite on IIS from http to https is not working,
我有个问题。 在IIS
我得到了一个带有两个端口80
和443(https)
。 我想将来自用户的所有http
请求重定向到https
。 我还Rewrite rule to https
添加了Rewrite rule to https
,但是当我在浏览器中输入http://localhost/site
它给了我相同的页面。 我需要将用户重定向到httpS://localhost/site
。
也许这是因为我的本地配置?
我在IIS
上禁用了Require SSL
。
The rule is:
<rewrite>
<rules>
<rule name="HTTPS Redirect">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
谢谢你。
以下是我们在生产 IIS 7 站点上使用的将所有请求从 HTTP 重定向到 HTTPS 的确切规则
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
您发布的内容与我们使用的内容之间存在一些细微差别。 此外,由于您在本地主机上运行,因此您不会将内置 Web 服务器与 Visual Studio 一起使用吗? 我认为它不会处理 IIS 重写规则。
我意识到这可能不是您的问题,但是我遇到了由不同问题引起的类似故障。
我启用了需要 SSL,这导致站点不断返回 403。因此,要使用此方法,您似乎必须禁用 SSL 设置 -> 需要 SSL。
希望这可以帮助某人。
此外,如果您有多个规则,则顺序可能很重要。 请务必在其他规则之前添加重定向规则,否则重定向可能不会触发。
这是使用需要规则顺序的 SPA 的示例。
<rules>
// Adding this as last rule will cause it to not redirect
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="static dist files" stopProcessing="true">
<match url="^(.+)" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
<rule name="index.html as document root" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/app/" />
</rule>
<rule name="SPA Routes" stopProcessing="true">
<match url=".*|.*/.*$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/app/" />
</rule>
</rules>
三点:
OFF
而不是^OFF$
url="https://{HTTP_HOST}/{REQUEST_URI}"
redirectType=Permanent
而不是Found
虽然两者都在工作,但在这种情况下最好使用Permanent
类型网络配置代码
<rewrite>
<rules>
<rule name="Redirect to HTTPS" 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>
您可以将此添加到您的页面。 强制重定向。
if (!Request.IsSecureConnection)
{
Uri uri = new Uri(Request.Url, Request.RawUrl);
Response.Redirect(string.Format("https://{0}{1}{2}", uri.Host.StartsWith("www.x.com") ? uri.Host : "www.x.com", uri.AbsolutePath, uri.Query));
}
我刚看到你的 mvc 标签
将此属性添加到您的操作中。 或控制器。
[RequireHttps]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.