【问题标题】:URL Rewrite on IIS from http to https is not working,在 IIS 上从 http 到 https 的 URL 重写不起作用,
【发布时间】:2012-10-13 06:05:45
【问题描述】:

我有问题。在IIS 上,我有一个带有两个端口80443(https) 的网站。我想将用户的所有http 请求重定向到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>

谢谢。

【问题讨论】:

    标签: asp.net-mvc http iis-7 https url-rewriting


    【解决方案1】:

    以下是我们在生产 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 重写规则。

    【讨论】:

    • 是的,你是对的。我已经解决了这个问题。问题在于操作中的 url 不正确。谢谢。
    • @NickitaFabregas 太棒了——很高兴你明白了。您应该自己发布并标记答案,或者将上述解决方案之一标记为答案,这样对于将来可能会偶然发现此问题的人来说,它不会成为一个未解决/未回答的问题!
    • 这个配置应该放在哪个文件中?
    • @PabloKarlsson 这将在您的根级别 web.config 文件中。我已更新示例以包含所有父 web.config 节点。
    【解决方案2】:

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

    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]
    

    【讨论】:

    • 该问题专门要求 IIS 配置,提供的答案超出范围。
    【解决方案3】:

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

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

    希望这对某人有所帮助。

    【讨论】:

      【解决方案4】:

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

      这是一个使用需要规则顺序的 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>
      

      【讨论】:

        【解决方案5】:

        它适用于 IIS 8.5 redirect

        三点:

        1. 使用单个单词OFF 而不是^OFF$
        2. 使用url="https://{HTTP_HOST}/{REQUEST_URI}"
        3. 使用redirectType=Permanent 而不是Found 虽然两者都可以工作,但在这种情况下最好使用Permanent 类型

        webconfig 代码

        <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>
        

        【讨论】:

          猜你喜欢
          • 2016-03-20
          • 2012-07-23
          • 2021-02-22
          • 1970-01-01
          • 1970-01-01
          • 2022-08-12
          • 2015-08-10
          • 2012-12-23
          • 1970-01-01
          相关资源
          最近更新 更多