删除多个正斜杠

Dan*_*lis 6 iis rewrite url-rewriting

我注意到,使用.NET MVC站点,您可以使用多个正斜杠命中URL,例如:

http://www.example.com//category
http://www.example.com//category//product
Run Code Online (Sandbox Code Playgroud)

URL加载正常,一切正常,但是,我被要求阻止这种情况发生.

我一直在尝试使用IIS URL重写来使其工作:

<rewrite>
    <rules>
        <rule name="Remove multiple slashes" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{UNENCODED_URL}" matchType="Pattern" pattern="^(.*)//(.*)$" />
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
         </rule>
    </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

然而,结果似乎很有气质.有时产品URL会重定向,有时它不会和类别一样.这几乎就像应用程序正在缓存URL一样.

有没有人知道我是否可以禁用任何缓存,或者是否有其他方法来解决这个多次斜杠问题?

任何帮助深表感谢.

Dan*_*lis 6

最后,我已经使用重定向后面的代码来使其工作.

我使用IIS URL重写的问题是由于IIS缓存重定向的方式.当我完全禁用缓存时,正如WouterH建议的那样,它起作用了.但是,我不习惯以这种方式禁用缓存,因为它可能会引入性能问题.

我的修复是在Global.asax.cs文件中使用重定向后面的代码:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string requestUrl = Request.ServerVariables["REQUEST_URI"];
    string rewriteUrl = Request.ServerVariables["UNENCODED_URL"];
    if (rewriteUrl.Contains("//") && !requestUrl.Contains("//"))
        Response.RedirectPermanent(requestUrl);
}
Run Code Online (Sandbox Code Playgroud)

我本来希望使用IIS URL Rewrite来实现这一点,不幸的是我没有时间继续沿着那条线行进.

有趣的是,下面的方法确实有效,但是,HTTP_X_REWRITE_URLHelicon ISAPI Rewrite添加了我在本地运行,但在我们的生产服务器上不可用.

<rewrite>
  <rules>
    <rule name="Remove multiple slashes" stopProcessing="true">
      <match url=".*" />
      <action type="Redirect" url="{REQUEST_URI}" />
      <conditions>
        <add input="{HTTP_X_REWRITE_URL}" pattern="([^/]*)/{2,}([^/]*)" />
      </conditions>
    </rule>
  </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)