加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – IIS URL重写:强制规范主机名和HTTP到HTTPS重定向

发布时间:2020-12-15 23:16:30 所属栏目:asp.Net 来源:网络整理
导读:我在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="h
我在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}服务器变量已经包含完整的querystring;默认情况下该选项为“true”,并在此处导致重复的查询字符串.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读