Asp.net Windows Server Web.config条索引文件名

Asp.net Windows Server Web.config条索引文件名,asp.net,iis,web-config,Asp.net,Iis,Web Config,我正在使用以下web.config文件将网站的非www版本重定向到www版本。但是,我也希望它能去掉索引文件的文件名 例如: 将www.example.com/index.html重定向到www.example.com <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <modules runAllManagedModulesForAllReq

我正在使用以下web.config文件将网站的非www版本重定向到www版本。但是,我也希望它能去掉索引文件的文件名

例如: 将www.example.com/index.html重定向到www.example.com

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

编辑:

这是我更新的配置文件。但是,现在它导致了500个错误


参见下面CodingGorilla的答案:)

为了在重定向后摆脱
index.html
,请删除
{R:1}
。但是,您需要修改该规则,使其仅对/index.html请求触发,并创建一个新规则,该规则在包含
{R:1}
的其他页面上触发,以便对例如.com/mypage.html的请求仍然正确重定向

编辑:

编辑#2

最后的答案是

根据我们的聊天对话,我认为这是最终的规则集:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1" stopProcessing="true">
                    <match url="index\.htm(?:l)?" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="example\.com$" />
                        </conditions>
                        <action type="Redirect" url="http://www.example.com/" />
                </rule>
                <rule name="CanonicalHostNameRule2" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

为了摆脱重定向后的
index.html
,请删除
{R:1}
。但是,您需要修改该规则,使其仅对/index.html请求触发,并创建一个新规则,该规则在包含
{R:1}
的其他页面上触发,以便对例如.com/mypage.html的请求仍然正确重定向

编辑:

编辑#2

最后的答案是

根据我们的聊天对话,我认为这是最终的规则集:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1" stopProcessing="true">
                    <match url="index\.htm(?:l)?" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="example\.com$" />
                        </conditions>
                        <action type="Redirect" url="http://www.example.com/" />
                </rule>
                <rule name="CanonicalHostNameRule2" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>