仅为顶级域创建非 www 到 www 的 IIS 重写规则

Create IISRewrite rule for non www to www for toplevel domain only

我正在尝试将我的非 www 用户重定向到 www 版本。但仅适用于我们的顶级域。

这是我当前的规则:

<rule name="Redirect Non WWW" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="^(?!www)(.*)domainname\.com$" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="http://www.{C:1}/{R:0}" />
      </rule>

这行得通,但不幸的是有点太好了。我们还有一些子域:https://subdomain.domainname.com。该规则还会重定向这些,因为它们不包含 www。

如何让我的重写规则只将访问者重定向到 https://domainname.com/ 而不是我们的子域?

      <rule name="Redirect Non WWW" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="^domainname\.com$" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="http://www.{C:1}/{R:0}" />
      </rule>