环境:
挑战
重写规则是全新的,我需要实现一个做两件事的规则:
问题是我本身无法真正测试这个,因为我们的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 ...但实际的重写规则从未在答案中发布!
答案 0 :(得分:2)
使用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" />
</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