使用 URL 重写的重定向过多

Too many redirects with URL Rewrite

我为适用于此域的重定向添加了以下配置,但对于 www 版本,它不会重定向到站点的 https 版本

    <rule name="Site Http" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="test.example.uk" />   
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:0}" />
    </rule>
    <rule name="Site to HTTPS" stopProcessing="true">
        <match url="(.*)" />
        <conditions trackAllCaptures="true">
            <add input="{HTTP_HOST}" pattern="test.example.uk" />
            <add input="{HTTP_HOST}" pattern="www.example.com" />
            <add input="{HTTP_HOST}" pattern="^((www\.)?example.com)$" />
            <add input="{HTTPS}" pattern="off" />
        </conditions>
        <action type="Redirect" url="https://{C:1}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent"/>
    </rule>

因此,无论我列出了哪个域,我都希望它转到网站的 https 版本(https://www.example.com) but if i enter domain.com it redirects to https, if i enter test.domain.uk it goes to https but if i enter www.example.com 它仍然在 http 上?

如果我添加

<add input="{HTTP_HOST}" pattern="www.example.com" />

关于 Site Http 规则,我在浏览器中收到太多重定向错误。

如何将 www.example.com 重定向到 https?

你可以尝试使用这个URL重写规则:

<rewrite>
  <rules>
    <rule name="Test" stopProcessing="true">
        <match url="^(.*)$" ignoreCase="false" />
        <conditions>
            <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />
         </conditions>
      <action type="Redirect" url="https://{SERVER_NAME}{URL}" redirectType="Found" />
    </rule>
  </rules>
</rewrite>

下面规则的意思是,如果HTTP头X-Forwarded-Proto中包含“https”,则该规则不会执行重定向。

<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" ignoreCase="false" />

此规则将避免循环重定向。