Url 重写 - Web.config - HTTPS 和 www

Url Rewrite - Web.config - HTTPS and www

我需要能够重定向到 HTTPS(当前有效)和 www(如果它不在 URL 中)。

如果 URL 没有 www,那么它工作正常。但是,当 URL DOESwww 时,它会重定向并添加一个额外的 www,例如 https://www.www.domain.com

请注意,我不想对域进行硬编码,而是想使用 HTTPHOST 或等效的东西。

当前重写规则:

    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="WWW Rewrite" enabled="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>

以这个正则表达式结束 - ^((?!www).)*[a-zA-Z0-9]+$

我还必须进入 IIS 并将 "Does not match the pattern" 更改为 "Matches the pattern"

    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="WWW Rewrite" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^((?!www).)*[a-zA-Z0-9]+$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>