4

I currently have a website where guests are able to access each url with any number of slashes to separate folder names. For example, if a URL is supposed to be:

http://example.com/one/two/three/four 

Then users could access the same page via any of the following:

http://example.com/one//two///three////four///// http://example.com/one/two////three/four///// http://example.com///one///////////two////three/four/ http://example.com///////////one///////////two/three/four 

However, I want the above example urls to only redirect users to this URL:

http://example.com/one/two/three/four 

This is my .htaccess file to attempt to stop the enormous slashes:

RewriteCond %{ENV:REDIRECT_STATUS} !^$ RewriteRule .* - [L] RewriteRule ^(.*)/+$ /$1 [R=301,L,NC] RewriteCond %{REQUEST_URI} ^/+(.*)/+$ RewriteRule .* /%1 [R=301,L] 

The third line successfully stops trailing slashes on long URLs. The 4th and 5th lines are my attempt to stop trailing slashes right after the domain name, but that was unsuccessful.

The reason why I ask this question is because I don't want google to catch me for duplicate content and with adsense active on the site, google will likely scan all the URLs that I access.

Is there a RewriteCond/RewriteRule combo I can use to strip the middle slashes or is it more involved?

2 Answers 2

22

You can use this rule for removing multiple slashes anywhere in URL except query string:

RewriteCond %{THE_REQUEST} \s[^?]*// RewriteRule ^.*$ /$0 [R=302,L,NE] 
Sign up to request clarification or add additional context in comments.

8 Comments

e.g. https://localhost///////////one///////////two//////////three/four/////// will become https://localhost/one/two/three/four/ in a single redirect.
Definitely works, @Mike you should accept this answer. Thanks anubhava.
It will work with any number of slashes more than one
@MarisB. It is not always easy to find documentation reference for all the features. Documentation is scattered in multiple places but this is an established fact that multiple // get converted to single / in the pattern of RewriteRule
@MarisB. It's not the $0 backreference itself that reduces multiple slashes. As you state, $0 simply contains the whole string matched by the pattern. In a directory (or .htaccess) context, the RewriteRule pattern matches against the URL-path after it has been mapped to the filesystem. It is the process of mapping the request to the filesystem that reduces multiple slashes. Conversely, the same directive does not work if used in a server or virtualhost context - which is processed before the request is mapped to the filesystem (when multiple slashes have not been reduced).
|
1

This works for me:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.