
我必須將需要託管在 IIS 伺服器上的 Angular 應用程式的完整 URL 從 HTTP 重新導向到 HTTPS。
我創建了以下規則,但它不起作用,它僅適用於主網域(例如http://www.somedomain.com,它重定向得很好,但是http://www.somedomain.com/route
這是我的規則。我究竟做錯了什麼?
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="./index.html" />
</rule>
</rules>
這個想法是重定向http://www.somedomain.com/route/something或者http://somedomain.com/route/something到https://www.somedomain.com/route/something
答案1
有多種方法可以執行相同的重定向。如果您使用 url="https://{HTTP_HOST}/{R:1}" 您必須新增appendQueryString="true",否則將不會附加 URI 路徑。
所以配置應該如下圖所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
但我通常使用另一個帶有 301 重定向的規則,在某些情況下瀏覽器可以更好地處理該規則。在這種情況下,您必須使用另一種類型的 uri: url="https://{HTTP_HOST}{REQUEST_URI}"
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS force" enabled="true" 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>
</system.webServer>
</configuration>
答案2
你做得對,但 IIS 有它的有線緩存,所以有時你不會立即看到變化,據我記得它可以在 iisreset 中倖存下來。此外,谷歌瀏覽器還發現可以記住重定向。
所以建議稍等一下或重新啟動兩台機器(我知道這聽起來很瘋狂)
這是我的工作配置。相同的想法但不同的條件。
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{SERVER_PORT}" pattern="443" negate="true" />
<add input="{HTTP_HOST}" pattern="\.local$" negate="true" />
<add input="{URL}" pattern="/robots.txt" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>