删除多个正斜杠 [英] Remove multiple forward slashes

查看:145
本文介绍了删除多个正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've noticed that with .NET MVC sites, you're able to hit URLs with multiple forward slashes, for example:

http://www.example.com//category
http://www.example.com//category//product

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

The URL loads fine and everything works, however, I've been asked to stop this from happening.

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

I've been trying to use IIS URL Rewrites to get it working:

<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>

然而,结果似乎非常有气质。有时产品网址会重定向,有时不会重定向,类别也会发生同样的事情。它几乎就像应用程序正在缓存URL一样。

However, the results seem very temperamental. Sometimes the product URL will redirect, sometimes it won't and the same thing happens with the category. It's almost like the URL is being cached by the application.

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

Does anyone know if I can disable any caching that's in place, or if there is another way to get around this multiple slash issue?

非常感谢任何帮助。

推荐答案

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

In the end, I have resorted to using a code behind redirect to get it working.

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

The issues I was having using the IIS URL Rewrites was due to the way IIS caches the redirects. When I disabled caching completely, as WouterH suggested, it worked. However, I'm not comfortable disabling caching in this way as it could introduce performance issues.

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

My fix was to use a code behind redirect in the Global.asax.cs file:

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);
}

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

I would have liked to use IIS URL Rewrite to get this working, unfortunately I didn't have the time to continue down that line.

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

Interestingly, the below method did work, however, the HTTP_X_REWRITE_URL is added by the Helicon ISAPI Rewrite which I'm running locally, but is not available on our production server.

<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>

这篇关于删除多个正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆