簡體   English   中英

IIS 上從 http 到 https 的 URL 重寫不起作用,

[英]URL Rewrite on IIS from http to https is not working,

我有個問題。 IIS我得到了一個帶有兩個端口80443(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>

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

您可以將此添加到您的頁面。 強制重定向。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM