Issue In Removing Double Or More Slashes From URL By .htaccess
Asked Answered
W

8

21

I am using the following htaccess rul to remove double or more slashes from web urls:

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

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

Its being redirected to proper single slahs url:

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:

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.

Wariness answered 13/6, 2013 at 6:34 Comment(0)
C
21

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]
Cy answered 29/8, 2013 at 17:24 Comment(3)
Based off your answer, to replace multiple hyphens, I did: RewriteCond %{REQUEST_URI} ^(.*)--(.*)$ RewriteRule . %1-%2 [R=301,L] --works, thanksUltracentrifuge
@Cy It doesn't seem to keep the query params in the url.Mcevoy
The first solution ^[A-Z]{3,}\s/{2,} works with N number of slashes without fail. However improvment suggested fails depending on number of slashes in the URL. Hence improvment is not useful instead buggy.Vocative
A
8

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. Additionally, Marcels answer lacks explanation, which I wanted to fix:

Rule 1: {THE_REQUEST} contains something like GET /index.html HTTP/1.1 (see docs). 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.

Rule 2: The regular expression ^(.*)/{2,}(.*)$ splits the request URI on multiple slashes. %1/%2 then combines the two splitted strings again, but with only one slash at this time.

Affiant answered 2/2, 2018 at 10:32 Comment(3)
but assume the following: localhost/hey/thanks/a/lot////////// is executed, the result is localhost/hey/thanks/a/lot/ i want to remove the final slashSkillet
@AlanDeep This is a different issue with a different solution.Affiant
This doesn't seem to be able to handle more complex cases with multiple occurrences of multiple slashes in the URL - e.g. ///some//more///complicated///path//Disclaimer
B
8

As per this link, following code should take care of extra slashes(anywhere) in URL.

RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ $0 [R=302,L,NE]
Balsamiferous answered 9/5, 2018 at 14:50 Comment(1)
The regex // will create a redirect loop (when compared against THE_REQUEST) if multiple slashes occur anywhere in the query string. The linked answer has been updated to use \s[^?]*// - which ensures multiple slashes are only matched in the URL-path, not the query string (if present).Turbellarian
P
3

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?

Polluted answered 19/10, 2013 at 23:30 Comment(2)
will not work if url is domain.com// - the redirected url will be the same!Patten
This answer is good enough and works with the given example. In your case use RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)?$ to declare as optional the last group of characters. Next time please ask before voting down.Polluted
S
2

The situation is simple to resolve using a tasty slice of .htaccess. All you need to do is copy the following code and your site’s root .htaccess file:

<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

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.

Rule 2: The regular expression ^(.*)/{2,}(.*)$ splits the request URI on multiple slashes. %1/%2 then combines the two splitted strings again, but with only one slash at this time.

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/

So basically it removes the double slashes from any URL.

Source From: https://www.meysmahdavi.com/

Swinish answered 21/9, 2021 at 5:37 Comment(1)
This is great! Had stumped me for a while. This addressed my issue where only one set of multiple slashes was being address, e.g.: example.com///page-1///page-2Hemiterpene
M
0

Just put the below code to your .htaccess file. it will remove multiple slash from anywhere . at the end of url and in from middle of 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>
Mufi answered 20/7, 2021 at 6:26 Comment(0)
W
0

Here is a slight variation that I have found works better when you have one .htaccess file and many subdirectories:

# 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]
Weathered answered 30/6, 2023 at 21:32 Comment(0)
A
0

Remove several slashes at once

RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{THE_REQUEST} (\s/{2,}.*)|(\s/.+//.*)
RewriteRule (.*) $1 [QSA,R=301,L]
Arlana answered 18/4 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.