使用.htaccess从URL中移除双斜杠或更多斜杠的问题

20

我正在使用以下htaccess规则来从网址中删除双斜线或更多:

#remove double/more slashes in url
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

如果斜杠出现在URI中间,例如,使用以下URL,则一切正常:

http://demo.codesamplez.com/html5//audio

它正在被重定向到正确的单斜杠URL:

http://demo.codesamplez.com/html5/audio

但是,如果URL在域名之后紧接着包含双斜杠,则它将无法工作,例如:

http://demo.codesamplez.com//html5/audio

它没有被重定向。

我该如何修复上述规则以使其适用于这种类型的URL?谢谢。

7个回答

21

试试这个:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]

它应该重定向到域名末尾的单斜杠。 并对你的进行改进:

RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]

根据您的回答,为了替换多个连字符,我做了以下操作:RewriteCond %{REQUEST_URI} ^(.)--(.)$ RewriteRule . %1-%2 [R=301,L]。--有效,谢谢。 - Michael d
1
@Marcel,它似乎没有在URL中保留查询参数。 - Jun
第一个解决方案^[A-Z]{3,}\s/{2,}在任意数量的斜杠下都能正常运行。然而,建议的改进会根据URL中的斜杠数量而失败。因此,改进不仅没有用处,而且可能存在错误。 - Sharad Upadhyay

8

对我来说,以下规则非常有效:

<IfModule mod_rewrite.c>
    RewriteBase /

    # rule 1: remove multiple leading slashes (directly after the TLD)
    RewriteCond %{THE_REQUEST} \s/{2,}
    RewriteRule (.*) $1 [R=301,L]

    # rule 2: remove multiple slashes in the requested path
    RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
    RewriteRule (.*) %1/%2 [R=301,L]
</IfModule>

这个想法在Marcels的回答基础上(谢谢!)有所改进,更加轻量,并包括 RewriteBase,如果您使用特定的子目录结构,则可能会有所帮助。此外,Marcels的回答缺乏解释,我想进行修复:
规则1: {THE_REQUEST} 包含类似于 GET /index.html HTTP/1.1 的内容(请参见文档)。因此,如果我们匹配第一个空格(\s)后面跟着多个斜杠(/{2,}),我们可以通过 $1 访问没有前导双斜杠的正确 URL。
规则2:正则表达式 ^(.*)/{2,}(.*)$ 将请求 URI 拆分为多个斜杠。然后,%1/%2再次组合两个拆分的字符串,但这次只有一个斜杠。

假设执行了 localhost/hey/thanks/a/lot//////////,结果是 localhost/hey/thanks/a/lot/。我想要移除最后的斜杠。 - Alan Deep
@AlanDeep 这是一个不同的问题,需要不同的解决方案 - Simon
1
这似乎无法处理具有多个斜杠的更复杂情况的URL的多个出现,例如 ///some//more///complicated///path// - Petr H

8
根据这个链接,以下代码应该能够处理URL中的额外斜杠(无论在哪里)。
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ $0 [R=302,L,NE]

3
正则表达式 // 如果在查询字符串中出现多个斜杠,将会创建一个重定向循环(与 THE_REQUEST 进行比较)。已更新链接的答案以使用 \s[^?]*// - 确保仅在 URL 路径中匹配多个斜杠,而不是查询字符串(如果存在)。 - MrWhite

3
为了避免在URL中出现长时间的字符重复,例如:
http://demo.codesamplez.com/html5///////////////////////////////////////////audio

你可以做以下事情:

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]

它应该与以下内容兼容:

http://demo.codesamplez.com//html5/audio

参见: .htaccess - 如何从URL中删除重复字符?


1
如果URL是domain.com//,它将无法工作-重定向的URL将保持不变! - dude
这个答案已经足够好了,并且在给定的示例中可以正常工作。在你的情况下,使用RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)?$来将最后一组字符声明为可选项。下次请在投票之前先询问。 - RafaSashi

2
这种情况很容易通过使用一小段美味的.htaccess文件来解决。你只需要复制以下代码并将其粘贴到你网站根目录下的.htaccess文件中即可:.htaccess
<IfModule mod_rewrite.c>
RewriteBase /
# Rule 1: remove multiple leading slashes (directly after the TLD)
RewriteCond %{THE_REQUEST} \s/{2,}
RewriteRule (.*) $1 [R=301,L]
# Rule 2: remove multiple slashes in the requested path
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule (.*) %1/%2 [R=301,L]
</IfModule>

规则1: {THE_REQUEST} 包含类似于 GET /index.html HTTP/1.1 的内容。

因此,如果我们匹配第一个空格 (\s) 后面跟着多个斜杠 (/{2,}),我们可以通过 $1 访问正确的URL,而不需要前导双斜杠。

规则2: 正则表达式 ^(.*)/{2,}(.*)$ 将请求URI拆分为多个斜杠。然后,%1/%2 再次将两个拆分的字符串组合在一起,但这次只有一个斜杠。

例如,此指令将进行以下重定向:

https://www.meysmahdavi.com// 重定向到 https://www.meysmahdavi.com/ https://www.meysmahdavi.com//blog-post/ 重定向到 https://www.meysmahdavi.com/blog-post/ https://www.meysmahdavi.com//path/directory/ 重定向到 https://www.meysmahdavi.com/path/directory/

基本上它会从任何URL中删除双斜杠。

来源:https://www.meysmahdavi.com/


1
太棒了!让我困惑了一段时间。这解决了我的问题,只有一个多个斜杠的集合被处理,例如:example.com///page-1///page-2。 - undefined

0
只需将以下代码放入您的 .htaccess 文件中,它将从任何位置删除多个斜杠。在 URL 末尾和 URL 中间都可以。
<IfModule mod_rewrite.c>
RewriteBase /`enter code here`
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=302,L,NE]
#Remove slash anywhere from url
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
# Rule 1: remove multiple leading slashes (directly after the TLD)
RewriteCond %{THE_REQUEST} \s/{2,}
RewriteRule (.*) $1 [R=301,L]
# Rule 2: remove multiple slashes in the requested path`enter code here`
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule (.*) %1/%2 [R=301,L]
</IfModule>

0
这里有一个稍微不同的变化,我发现当你有一个.htaccess文件和许多子目录时,它的效果更好:
# Remove multiple slashes anywhere in url

# rule 1: Remove multiple leading slashes directly after the domain name
RewriteCond %{THE_REQUEST} \s/{2,}
RewriteRule (.*) $1 [R=301,L]
 
# rule 2: Remove multiple slashes anywhere in the rest of the path
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L,NE]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接