簡體   English   中英

通過在web.config中重寫URL強制HTTPS

[英]Force HTTPS via URL rewrite in web.config

我正在嘗試通過web.config URL重寫規則對包含路徑的URL強制執行HTTPS。

例如,必須強制http://my.domain.com/folder使用https://my.domain.com/folder

我還要對相對路徑上的所有頁面強制使用HTTPS

例如,將http:// my.domain.com/folder/page.aspx強制設為https:// my.domain.com/folder/page.aspx

這就是我所擁有的:

<rewrite>
    <rules>
      <rule name="Redirect to https">
        <match url="(.*)"/>
        <conditions>
          <add input="{HTTPS}" pattern="Off"/>
          <add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
      </rule>
    </rules>
  </rewrite>

檢查外殼和所用報價的類型。 另外,第二個輸入法無效-模式對正則表達式無效。 該規則適用於您想要的:

<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="Permanent" />
</rule>

嘗試以下URL重寫規則:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <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>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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