从URL中删除尾随斜杠

Ash*_*ish 5 asp.net webforms web-config

我想将"abc.aspx /"重定向到"abc.aspx".如何才能做到这一点?

当请求的页面以'/'结尾时,页面会被破坏.如何处理此类请求?是否有可以添加到web.config文件中的重写规则?

Mih*_*scu 15

在你的web.config中system.webServer,添加

<rewrite>
  <rules>

    <!--To always remove trailing slash from the URL-->
    <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>
Run Code Online (Sandbox Code Playgroud)

一些陷阱

在您的开发环境中,如果您在Visual Studio开发服务器下运行您的网站,您将无法看到此功能正常工作.您需要将应用程序配置为至少在IIS Express下运行.

当您部署网站并发现此功能在生产服务器上不起作用时,这将是因为您错过了配置的内容.常见错误之一是在applicationHost.config文件中为overrideModeDefault属性设置属性.Deny<sectionGroup name="rewrite">

如果您在共享托管环境中并且发现此功能无效,请询问您的提供商是否已授予您配置此部件的权限.

资料来源:http://www.tugberkugurlu.com/archive/remove-trailing-slash-from-the-urls-of-your-asp-net-web-site-with-iis-7-url-rewrite-module