【发布时间】:2023-03-07 22:44:09
【问题描述】:
我想要的是所有非 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/
@ 987654323@
但是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,我仍然得到安全异常。
【问题讨论】:
标签: ssl redirect iis url-rewriting web-config