在 IIS 中,如何在没有太多重定向的情况下将我的所有 URL 转发到非 www、https 且没有文件扩展名

In IIS, how can I forward all my URLs to a non-www, https, and no file extension without having too many redirects

我在 Stack Overflow 中遇到过许多类似的问题,但我一直无法找到一个可以帮助我让所有这三个重定向规则一起工作的问题。

我正在尝试将我所有的 URL 转发为这种格式:

https://example.com/page

我需要 https,我不需要 www,我不希望 html 或 aspx 扩展名可见。这是我到目前为止所拥有的。 Chrome 说我的重定向太多,我该如何合并这些规则?

    <rewrite>
        <rules>
            <clear />

            <rule name="RewriteHTML">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="{R:1}.html" />
            </rule>

            <rule name="Redirect www to non-www" enabled="true" stopProcessing="true">
                <match url="(.*)\.html$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://example.com/{R:1}" redirectType="Permanent"/>
            </rule>

            <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)\.html$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
            </rule>

            <rule name="RemoveExtension" stopProcessing="true">
                 <match url="(.*)\.html$" />
                 <action type="Redirect" url="{R:1}" redirectType="Permanent" />
            </rule>

        </rules>
    </rewrite>

在尝试了模式和规则顺序以及 stopProcessing 命令等之后,我终于弄明白了。不再有重定向循环!我实际上想出了我在问什么等等。

  1. 从根域中删除 index.html
  2. 将 www 重定向到非 www
  3. 删除尾部斜杠
  4. 使 URL 始终小写
  5. 将 http:// 重定向到 https://
  6. 允许 URL 在没有 .html 扩展名的情况下工作
  7. 不带 .html 扩展名
  8. 重定向到 URL

我的代码:

<rules>         
    <!-- index.html Redirect -->    
    <rule name="index.html Redirect" enabled="true">
        <match url="^(.*\/)*index\.html$" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
        </conditions>
        <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule> 

    <!-- Redirect WWW to non-WWW -->
    <rule name="Redirect WWW to non-WWW" enabled="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
        </conditions>
        <action type="Redirect" url="http://example.com/{R:1}" />
    </rule>

    <!-- Remove Trailing Slash -->
    <rule name="Remove Trailing Slash" enabled="true">
        <match url="(.*)/$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{R:1}" />
    </rule>

    <!-- Make URL Lower Case -->
    <rule name="Make URL Lower Case" enabled="true">
        <match url="[A-Z]" ignoreCase="false" />
        <action type="Redirect" url="{ToLower:{URL}}" />
    </rule>

    <!-- Redirect To HTTPS -->
    <rule name="Redirect To HTTPS" enabled="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTPS}" pattern="^OFF$" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>

    <!-- Redirect To URL With No Extension -->
    <rule name="Redirect To URL With No Extension" enabled="true">
        <match url="(.*)\.(html)" />
        <action type="Redirect" url="{R:1}" />
    </rule>             

    <!-- Rewrite URL For No Extension -->
    <rule name="Rewrite URL For No Extension" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="{R:0}.html" />
    </rule> 
</rules>

IIS 中的规则如下所示 URL 重写模块: