K
K
Kosstya2014-12-12 18:22:17
htaccess
Kosstya, 2014-12-12 18:22:17

How to set up a 301 redirect from the main with // or more to the main without slashes?

You need to set up a 301 redirect, which, if you enter more than two slashes on the main page, will redirect to the main page without slashes.
That is from:
site.com///
to
site.com
Now still site.com///
Found a solution for internal pages
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
But this code doesn't work for the main one.
I tried to put it above the rest of the rules - it did not help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Yakovenko, 2014-12-12
@Xazzzi

Actually, I will take out the answer from the comment:
Here is the only working config that removes multiple leading slashes:

RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ //+([^\ ]*) 
RewriteRule .* https://%{HTTP_HOST}/%1 [R=301,L]

I checked on my test site, here are a couple of examples:
https://squadpaint.com///////
https://squadpaint.com///////sometextafter
Options with %{REQUEST_URI} don't work, t .to. Apache glues multiple slashes into 1 even before passing the address to .htaccess for processing
. the pattern stops matching, the %{THE_REQUEST} line has no end =)
In the regexp above, you also need to take into account the possibility that the request may come with a domain in the address, I leave that to you.

K
Kosstya, 2014-12-12
@Kosstya

Doesn't want to. So tried
RewriteCond %{REQUEST_URI} ^\/+$
RewriteRule . %1/%2 [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question