gpt4 book ai didi

asp.net - IIS URL 重写 : Enforce canonical hostname & HTTP to HTTPS redirect

转载 作者:行者123 更新时间:2023-12-03 19:54:34 25 4
gpt4 key购买 nike

我在我的 web.config 文件中使用这两个规则:

<rule name="Enforce canonical hostname" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
<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>

通过这两个规则,我可以使用以下重定向工作:
  • http://www.example.com ---> https://www.example.com
  • http://example.com--- > https://www.example.com
  • https://example.com ---> 这无法重定向到 https://www.example.com ... 为什么?
  • 最佳答案

    不确定您是否仍在寻求答案,但这里是。经过一番搜索和反复试验,我发现通过以下规则取得了成功。我遇到的大多数示例在模式匹配方面都不必要地复杂,并引入了其他变量来阻止规则按预期工作。以下规则可以应用于任何网站,并且没有任何内容是硬编码的,因此它应该始终是直接复制和粘贴的工作:

    <rule name="Redirect to WWW" stopProcessing="true" >
    <match url="(.*)" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="^www\." negate="true"/>
    </conditions>
    <action type="Redirect" url="https://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
    </rule>
    <rule name="Redirect to HTTPS">
    <match url="(.*)" />
    <conditions>
    <add input="{HTTPS}" pattern="OFF"/>
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
    </rule>

    有两点需要注意:redirectType="Permanent"将导致规则被应用,直到浏览器的历史/缓存被清空;这应该是一件好事,因为浏览器会继续工作。此外, appendQueryString="false"是必要的,因为 {HTTP_URL} 服务器变量已经包含完整的查询字符串;默认情况下,该选项为“true”,并且会在此处导致重复的查询字符串。

    关于asp.net - IIS URL 重写 : Enforce canonical hostname & HTTP to HTTPS redirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9111774/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com