9

我有个问题。在IIS我得到一个有两个端口80443(https). 我想将http用户的所有请求重定向到https. 我还添加了Rewrite rule to https,但是当我在浏览器中输入时,http://localhost/site它给了我相同的页面。我需要将用户重定向到httpS://localhost/site.

也许这是因为我的本地配置?

我禁用Require SSLIIS.

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>

谢谢你。

4

5 回答 5

16

以下是我们在生产 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>

您发布的内容与我们使用的内容之间存在一些细微差别。此外,由于您在本地主机上运行,​​您不会使用带有 Visual Studio 的内置 Web 服务器吗?我认为它不会处理 IIS 重写规则。

于 2012-10-23T13:44:20.377 回答
7

我意识到这可能不是您的问题,但是我遇到了类似的崩溃,这是由另一个问题引起的。

我启用了要求 SSL,这导致站点不断返回 403。因此,要使用此方法,您似乎必须禁用 SSL 设置 -> 要求 SSL。

希望这可以帮助某人。

于 2013-10-05T19:12:40.773 回答
0

它适用于 IIS 8.5 重定向

三点:

  1. 使用单个单词OFF而不是^OFF$
  2. 利用url="https://{HTTP_HOST}/{REQUEST_URI}"
  3. 在这种情况下使用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>
于 2020-08-07T17:52:52.230 回答
0

此外,如果您有多个规则,则顺序可能很重要。确保在其他规则之前添加重定向规则,否则重定向可能不会触发。

这是一个使用需要规则顺序的 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>
于 2017-12-06T21:19:44.813 回答
-1

您可以将其添加到您的页面。强制重定向。

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]
于 2012-10-23T13:47:30.983 回答