IIS 重写规则将非 www 重定向到动态域等效且始终为 https

IIS rewrite rule Redirect Non-www to dynamic Domain Equivalent and always https

我想要的是所有非 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" 上,因为我希望此规则适用于多个域名。

然后我也检查了这个,它有一个简洁的规则:

<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个月没有登录所以才来问的)

此外,我还检查了,这似乎也是一个不错的选择:

<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 redirects to https://example.com 但我仍然遇到安全异常。

首先,我强烈建议您获取支持example.comwww.example.com 的新SSL 证书。这种证书实际上是大多数 SSL 提供商的标准证书,它不一定是通配符证书。否则你将无法像现在这样处理对 https://example.com 的请求,我认为这是一个问题。

您的前两条规则应如下所示。

P.S. 301 重定向被浏览器缓存了一段时间。 Google clear 301 redirect cache 在测试新规则之前适用于您的浏览器。

<rule name="All HTTP to HTTPS+WWW" stopProcessing="true">
    <match url=".*" />
    <conditions trackAllCaptures="true">
        <add input="{SERVER_PORT_SECURE}" pattern="0" />
        <add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
        <!-- here with this 3rd condition we capture the host name without "www." prefix into {C:1} variable to use in redirect action -->
        <add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
    </conditions>
    <action type="Redirect" url="https://www.{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>                
<rule name="All HTTPS With No WWW to HTTPS+WWW" stopProcessing="true">
    <match url=".*" />
    <conditions trackAllCaptures="false">
        <add input="{SERVER_PORT_SECURE}" pattern="1" />
        <add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
        <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
    </conditions>
    <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>