【问题标题】:IIS rewrite rule for http to https with canonical host带有规范主机的 http 到 https 的 IIS 重写规则
【发布时间】:2017-08-09 15:21:51
【问题描述】:

环境:

  • Windows Server 2012 R2
  • IIS 8.5

挑战

重写规则是全新的,我需要实现一个规则来做两件事:

  1. 将协议从 http 更改为 https
  2. 将主机名更改为 www.example.com

问题是我不能真正测试这个本身,因为我们的 SSL 证书只存在于我们的生产站点上,所以我需要确保我做对了。

在进行研究时,我发现有关重写规则的文档有些稀疏,但从各种示例等中拼凑出以下内容:

    <rule name=”http_to_https_redirect">
     <match url="(.*)" />
<conditions><add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
     <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
    </rule>

我仍然不知道 {R:1} 的含义或它的行为方式,因为我发现只有一个简短的说明,它是“对规则模式的反向引用由 {R:N} 标识,其中 N 是从 0 到 9。请注意,对于这两种类型的反向引用,{R:0} 和 {C:0} 将包含匹配的字符串。” (来自:https://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

问题:

根据上面的第 1 点和第 2 点,上述规则是否正确?

希望有更多经验的人可以确认 - 是/否?

还发现了这个帖子: IIS Url Rewrite rule HTTP to HTTPS AND add WWW ...但实际的重写规则从未在答案中发布!

【问题讨论】:

    标签: iis url-rewriting


    【解决方案1】:

    使用https和规范主机完成重定向的重写url如下:

      <rewrite>
       <rules>
        <rule name="redirect_http_to_https_with_canonical_host" stopProcessing="true">
         <match url="(.*)" />
         <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
           <add input="{HTTPS}" pattern="off" />
           <add input="{HTTP_HOST}" pattern="www\.example\.com" negate="true" /> 
         </conditions>
         <action type="Redirect" url="https://www.example.com{URL}" appendQueryString="true" redirectType="Permanent" />
        </rule>
       </rules>
      </rewrite>
    

    以上适用于以下情况:

    Input:  http://example.com
    Result: https://www.example.com
    
    Input:  http://www.example.com
    Result: https://www.example.com
    
    Input:  http://example.com/somepage.aspx   
    Result: https://www.example.com/somepage.aspx
    
    Input:  http://example.com/somepage.aspx?q=test
    Result: https://www.example.com/somepage.aspx?q=test
    
    Input:  https://example.com
    Result: https://www.example.com
    
    Input:  https://www.example.com
    Result: https://www.example.com
    
    Input:  https://example.com/somepage.aspx   
    Result: https://www.example.com/somepage.aspx
    
    Input:  https://example.com/somepage.aspx?q=test
    Result: https://www.example.com/somepage.aspx?q=test
    

    【讨论】:

    • 如何将其导入到我的 IIS url 重写中?
    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 2015-08-12
    • 2020-11-28
    • 2014-11-04
    • 2020-03-30
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多