[英]Issue In Removing Double Or More Slashes From URL By .htaccess
I am using the following htaccess rul to remove double or more slashes from web urls:我正在使用以下 htaccess 规则从 web url 中删除双斜杠或更多斜杠:
#remove double/more slashes in url
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
This is working fine for slashes occured in the middle of uris, such as, If use url:这对于 uri 中间出现的斜线工作正常,例如,如果使用 url:
http://demo.codesamplez.com/html5//audio
Its being redirected to proper single slahs url:它被重定向到正确的单斜线网址:
http://demo.codesamplez.com/html5/audio http://demo.codesamplez.com/html5/audio
But if the url contains double slashes in the beginning, JUST AFTER the domain name, then there its not working, example:但是如果 url 在开头包含双斜杠,就在域名之后,那么它就不起作用,例如:
http://demo.codesamplez.com//html5/audio
its not being redirected.它没有被重定向。
How I can fix the above rule to work for this type of urls as well?我如何修复上述规则以适用于此类网址? Thanks.
谢谢。
Give it a try with:试试看:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
It should redirect to a single slash at the end of the domain.它应该重定向到域末尾的单个斜杠。 And an improvement on yours:
以及对您的改进:
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
For me, the following rules work perfectly:对我来说,以下规则非常有效:
<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>
The idea is heavily based on Marcels answer (thanks!), but this one is a bit more lightweight and includes the RewriteBase
, which may be helpful if you work with specific subdirectory structures.这个想法在很大程度上基于 Marcels 的回答(谢谢!),但这个更轻量级并包含
RewriteBase
,如果您使用特定的子目录结构,这可能会有所帮助。 Additionally, Marcels answer lacks explanation, which I wanted to fix:此外,Marcels 的回答缺乏解释,我想解决这个问题:
Rule 1: {THE_REQUEST}
contains something like GET /index.html HTTP/1.1
(see docs ).规则 1:
{THE_REQUEST}
包含类似GET /index.html HTTP/1.1
(请参阅文档)。 Hence, if we match the first whitespace ( \\s
) followed by multiple slashes ( /{2,}
), we can access the correct URL without the leading double slash via $1
.因此,如果我们匹配第一个空格 (
\\s
) 后跟多个斜杠 ( /{2,}
),我们可以通过$1
访问正确的 URL,而无需前导双斜杠。
Rule 2: The regular expression ^(.*)/{2,}(.*)$
splits the request URI on multiple slashes.规则 2:正则表达式
^(.*)/{2,}(.*)$
在多个斜杠上拆分请求 URI。 %1/%2
then combines the two splitted strings again, but with only one slash at this time.然后
%1/%2
再次组合两个拆分的字符串,但此时只有一个斜杠。
To prevent long repetition of characters in your url such as:防止您的网址中出现长时间重复的字符,例如:
http://demo.codesamplez.com/html5///////////////////////////////////////////audio
you can do:你可以做:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
It should works with :它应该适用于:
http://demo.codesamplez.com//html5/audio
see also: .htaccess - how to remove repeated characters from url?另请参阅: .htaccess - 如何从 url 中删除重复字符?
Just put the below code to your .htaccess file.只需将以下代码放入您的 .htaccess 文件。 it will remove multiple slash from anywhere .
它将从任何地方删除多个斜线。 at the end of url and in from middle of url.
在 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>
The situation is simple to resolve using a tasty slice of .htaccess.使用 .htaccess 的美味切片很容易解决这种情况。 All you need to do is copy the following code and your site's root
.htaccess
file:您需要做的就是复制以下代码和您网站的根
.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>
Rule 1: {THE_REQUEST}
contains something like GET /index.html HTTP/1.1
规则 1:
{THE_REQUEST}
包含类似GET /index.html HTTP/1.1
Hence, if we match the first whitespace (\\s)
followed by multiple slashes (/{2,})
, we can access the correct URL without the leading double slash via $1
.因此,如果我们匹配第一个空格
(\\s)
后跟多个斜杠(/{2,})
,我们可以通过$1
访问没有前导双斜杠的正确 URL。
Rule 2: The regular expression ^(.*)/{2,}(.*)$
splits the request URI on multiple slashes.规则 2:正则表达式
^(.*)/{2,}(.*)$
在多个斜杠上拆分请求 URI。 %1/%2
then combines the two splitted strings again, but with only one slash at this time.然后
%1/%2
再次组合两个拆分的字符串,但此时只有一个斜杠。
So for example, this directive will redirect as follows:例如,该指令将重定向如下:
https://www.meysmahdavi.com// redirects to https://www.meysmahdavi.com/ https://www.meysmahdavi.com//blog-post/ redirects to https://www.meysmahdavi.com/blog-post/ https://www.meysmahdavi.com//path/directory/ redirects to https://www.meysmahdavi.com/path/directory/ 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/
So basically it removes the double slashes from any URL.所以基本上它会从任何 URL 中删除双斜杠。
Source From: https://www.meysmahdavi.com/来源: https : //www.meysmahdavi.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.