我想要的是所有非https或没有
www
预设的被重定向到:
"https://www." + domain name + possible query string parameters
.
我有一条重写规则(
found here
):
<rule name="non-www to www https" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>
但是,当在浏览器地址栏中键入以下域时,不会发生重定向(由于我没有通配符DNS SSL证书,因此我收到了一个安全证书错误):
https://example.com/
http://example.com/
但是
example.com
(无协议),正确重定向到
https://www.example.com/
还要注意,在上面的规则中,我正在动态匹配主机名,而不仅仅是在“example.com”上,因为我希望这个规则适用于多个域名。
然后我也检查了这个
post
,它有一个简洁的规则:
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
我认为这正是我想要的,但是我如何使本例中的域名动态并在重定向中保留它(就像第一个代码示例所做的那样)?(原始海报在过去6个月内没有登录,因此我在这里提问)
此外,我还检查了
this post
,这似乎也是一个很好的候选人:
<rule name="Redirect top domains with non-www to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
<serverVariables>
<set name="Redirect" value="false" />
</serverVariables>
</rule>
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
<add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
但是后来
http://example.com
重定向到
https://example.com
我仍然得到了安全例外。