IIS 7.5:删除尾部斜杠不起作用

时间:2013-10-23 06:49:14

标签: trailing-slash

我们有ASP.NET应用程序并在web.config中设置:

<rewrite>
  <rules>
    <rule name="RemoveTrailingSlashRule1" stopProcessing="true">
      <match url="(.*)/$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>
  </rules>
</rewrite>

但是,尾随的斜线仍在那里。

另外,我尝试使用Global.asax.cs文件中的代码:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.ToString().Contains("http://concert.local/elki/"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().Replace("http://concert.local/elki/", "http://concert.local/elki"));
    }
}

但是,它不起作用。

具体是“elki”是项目中的子文件夹,并且有自己的web.config文件,如下所示:

<configuration>
  <system.webServer>
    <defaultDocument>
      <files>
        <add value="Sections.aspx" />
      </files>
    </defaultDocument>
    <httpRedirect enabled="true" destination="/elki" httpResponseStatus="Permanent" />
  </system.webServer>
</configuration>

如何使它工作,即删除尾部斜杠?

1 个答案:

答案 0 :(得分:0)

尝试从web.config中删除它?

    <rewrite>
        <rules>
            <rule name="Remove trailing slash" stopProcessing="true">
                  <match url="^([^.]+)/$" />
                  <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  </conditions>
                  <action type="Redirect" redirectType="Permanent" url="{R:1}" />
            </rule>
        </rules>
    </rewrite>