繁体   English   中英

通过 .htaccess 从 URL 中删除双斜线或更多斜线的问题

[英]Issue In Removing Double Or More Slashes From URL By .htaccess

我正在使用以下 htaccess 规则从 web url 中删除双斜杠或更多斜杠:

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

这对于 uri 中间出现的斜线工作正常,例如,如果使用 url:

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

它被重定向到正确的单斜线网址:

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

但是如果 url 在开头包含双斜杠,就在域名之后,那么它就不起作用,例如:

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

它没有被重定向。

我如何修复上述规则以适用于此类网址? 谢谢。

试试看:

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

它应该重定向到域末尾的单个斜杠。 以及对您的改进:

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

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

<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再次组合两个拆分的字符串,但此时只有一个斜杠。

根据链接,以下代码应处理 URL 中的额外斜杠(任何地方)。

RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ $0 [R=302,L,NE]

防止您的网址中出现长时间重复的字符,例如:

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 中删除重复字符?

只需将以下代码放入您的 .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>

使用 .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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM