带有 HTTP 状态代码 301 的 IIS 重写规则

IIS rewrite rule with HTTP status code 301

I need to write a IIS rewrite rule which matches the following condition
  1. example.com
  2. www.example.com
  3. http://www.example.com
  4. https://www.example.com
  5. http://example.com
  6. https://example.com

    这一切都应该重定向到 https://www.example.com/somepage/

    条件是从 SEO 的角度来看,只应识别一个 301 重定向。任何指针表示赞赏

尝试了以下重定向,但会导致两跳而不是一跳

    <rule name="no path" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{REQUEST_URI}" pattern="^\/?$" ignoreCacom="true" />
        <add input="{HTTP_HOST}" pattern="^(www.)?example.com$" ignoreCacom="true" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/somepage" redirectType="Permanent" />
    </rule>
    <rule name="with path and http" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{PATH_INFO}" pattern="\/somepage" negate="true" ignoreCacom="true" />
        <add input="{REQUEST_URI}" pattern="^\/.+$" ignoreCacom="true" />
        <add input="{HTTP_HOST}" pattern="^(www.)?example.com$" ignoreCacom="true" />
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://www.example.com{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    <rule name="with path and https" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{PATH_INFO}" pattern="\/somepage" negate="true" ignoreCacom="true" />
        <add input="{REQUEST_URI}" pattern="^\/.+$" ignoreCacom="true" />
        <add input="{HTTP_HOST}" pattern="^example.com$" ignoreCacom="true" />
        <add input="{HTTPS}" pattern="^ON$" />
      </conditions>
      <action type="Redirect" url="https://www.example.com{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    <rule name="with somepage, no www" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{PATH_INFO}" pattern="\/somepage" ignoreCacom="true" />
        <add input="{HTTP_HOST}" pattern="^example.com$" ignoreCacom="true" />
      </conditions>
      <action type="Redirect" url="https://www.example.com{REQUEST_URI}" redirectType="Permanent" />
    </rule>
    <rule name="with somepage and https, no www" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{PATH_INFO}" pattern="\/somepage" ignoreCacom="true" />
        <add input="{HTTP_HOST}" pattern="^www.example.com$" ignoreCacom="true" />
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://www.example.com{REQUEST_URI}" redirectType="Permanent" />
    </rule>

enter code here

这些规则是否达到您的要求? 有了这个规则顺序。带有路径的 http(s)://(www.)example.com/a.aspx 将被重定向到 https://www.example.com/a.aspx

https://www.example.com/a.aspx 不会被重定向以避免无限重定向循环。

http(s)://(www.)example.com/ 将被重定向到 https://www.example.com/somepage

因为不可能将所有内容合并到一条规则中。我们至少需要 3 条规则才能实现。

   <rule name="https://www.example.com">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_URI}" pattern="^/.+$" />
                        <add input="{HTTP_HOST}" pattern="www.example.com" />
                        <add input="{HTTPS}" pattern="^on$" />
                    </conditions>
                    <action type="None" />
                </rule>
                <rule name="no path" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{URL}" pattern="^(/)?$" />
                        <add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
                    </conditions>
                    <action type="Redirect" url="https://www.example.com/somepage/" />
                </rule>
                <rule name="with path" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_URI}" pattern="^/.+$" />
                        <add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
                        <add input="{REQUEST_URI}" pattern="^/somepage" negate="true" />
                    </conditions>
                    <action type="Redirect" url="https://wwww.example.com{REQUEST_URI}" />
                </rule>