IIS上的URL从HTTP重写为HTTP不起作用



我有问题。在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>

谢谢。

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

我意识到这可能不是您的问题,但是我有类似的崩溃,这是另一个问题。

我已经启用了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>

它在IIS 8.5中对我有用重定向

三分:

  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>

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

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]

最新更新