簡體   English   中英

Web.config 將 www 和非 www 重定向到 https

[英]Web.config redirect of both www and non-www to https

I had success redirecting www.example.com to https://example.com but not both www.example.com and example.com. 這是我重定向兩者的代碼:

 <rewrite> <rules> <rule name="Force www and non-www" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAny" trackAllCaptures="false"> <add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.example.com" /> <add input="{HTTP_HOST}{REQUEST_URI}" pattern="example.com" /> </conditions> <action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent"/> </rule> </rules> </rewrite>

它似乎只喜歡 www 選項,因為它在 www 和非 www 或非 www 時都失敗。 代碼有問題嗎?

首先,為了排除瀏覽器緩存的影響,我們應該始終在瀏覽器隱身window中進行測試。 此外,請重新啟動服務器以應用最新更改。 Google Chrome默認禁用Http協議,請注意查看瀏覽器地址欄中實際的URL。
如果我們只想將 Http 重定向到 https,添加{HTTPS}服務器變量以匹配Non-http協議就足夠了。

        <rules>
    <rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
                    <add input="{HTTPS}" pattern="off" />
                    <add input="{HTTP_HOST" pattern="example.com|www.example.com" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>

為了強制使用WWW前綴,請參考以下規則。 請注意圖案模式。

   <rule name="Force www" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^example\.com$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com{REQUEST_URI}" />
    </rule>

如果問題仍然存在,請隨時告訴我。

暫無
暫無

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

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