我们有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>
如何使它工作,即删除尾部斜杠?
答案 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>