2017-03-17 90 views
-1

环境:IIS为HTTP重写规则到https与规范的主机

  • 的Windows Server 2012 R2
  • IIS 8.5

挑战

完全新的重写规则我需要实现一个可以做两件事的规则:

01从http
  1. 更改协议HTTPS来www.example.com
  2. 更改主机名

问题是我不能真正检验这个本身,因为我们的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.请注意,对于这两种类型的后向引用,{ (来自: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 ...但实际的重写规则从未张贴在答案!

回答

0

重写URL使用HTTPS来完成重定向和规范的主机如下:

<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" /> 
    <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