删除多个正斜杠

问题描述 投票:0回答:4

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

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

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

我一直在尝试使用 IIS URL Rewrites 来让它工作:

<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 会重定向,有时不会,同样的事情也会发生在类别上。这几乎就像应用程序正在缓存 URL。

有谁知道我是否可以禁用任何现有的缓存,或者是否有另一种方法来解决这个多斜线问题?

非常感谢任何帮助。

iis url-rewriting
4个回答
6
投票

最后,我求助于使用代码隐藏重定向来让它工作。

我在使用 IIS URL Rewrites 时遇到的问题是由于 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);
}

我本来想使用 IIS URL Rewrite 来让它工作,不幸的是我没有时间继续那条线。

有趣的是,下面的方法确实有效,但是,

HTTP_X_REWRITE_URL
是由我在本地运行的 Helicon 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>

3
投票

URL 重写模块

由于 IIS 自动规范化带有双斜杠的 url,您可以尝试重定向到规范化的 url,如下所示:

<rewrite>
  <rules>
    <rule name="Remove multiple slashes" stopProcessing="true">
      <match url=".*" />
      <action type="Redirect" url="{REQUEST_URI}" />
      <conditions>
        <add input="{UNENCODED_URL}" pattern="(.*?)[/]{2,}$" />
      </conditions>
    </rule>
  </rules>
</rewrite>

您也可以尝试禁用URL重写模块的缓存

禁用重写规则的内部缓存

在 URL 重写模块运行中禁用入站重写规则的缓存 在提升的命令提示符下执行以下命令:

reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Rewrite /v
  RewriteCacheEnabled /t REG_DWORD /d 0

我有种小感觉,在此更改后您必须重新启动网络服务器 :)

其他选项#1:不可缓存的服务器变量

脑子里突然冒出这个想法:

在规则中使用不可缓存的服务器变量,例如试试

HTTP_USER_AGENT

<rewrite>
  <rules>
    <rule name="Remove multiple slashes" stopProcessing="true">
      <match url=".*" />
      <action type="Redirect" url="{REQUEST_URI}" />
      <conditions>
        <add input="{UNENCODED_URL}" pattern="(.*?)[/]{2,}$" />
        <add input="{HTTP_USER_AGENT}" pattern=".*" />
      </conditions>
    </rule>
  </rules>
</rewrite>

您可以在这里探索其他服务器变量

其他选项#2:清除浏览器缓存

在 web 开发过程中,确保使用 Ctrl+F5 刷新页面,或者在进行更新重写规则等更改后清除浏览器缓存。否则你可能会花几个小时观察同样的问题,而只是你的浏览器需要刷新其缓存。

其他选项 #3:IIRF

如果您真的无法使用 IIS URL 重写模块,您可以试试开源 ISAPI 模块IIRF。它接受类似于 Apache

mod_rewrite
的规则。


0
投票

尝试以下

    <rule name="RemoveMultipleSlashes" patternSyntax="ECMAScript" stopProcessing="true">
        <match url=".*" />
        <action type="Redirect" url="{REQUEST_URI}" />
        <conditions>
            <add input="{UNENCODED_URL}" pattern="([^/]*)/{2,}([^/]*)" />
        </conditions>
    </rule>

0
投票

我们可以在web.config文件中使用Below IIS Rewrite Rule。我已经从 Google Search Console 解决了这个问题,我用下面的代码永久修复了它。

下面的代码将删除 Url 中任何位置的双斜杠。

<rewrite>
  <rules>
    <rule name="RemoveDoubleSlash" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="(.*)" />
        <action type="Redirect" url="{C:1}/{C:2}" />
        <conditions>
            <add input="{UNENCODED_URL}" pattern="(.*)//(.*)" />
        </conditions>
    </rule>
  </rules>
</rewrite>
© www.soinside.com 2019 - 2024. All rights reserved.