![](/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.