2

I can remove multiple slashes anywhere in URL using:

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

But it doesn't work for multiple slashes after the domain

I have tried

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

and

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

both produce the expected rewirting when going from

domain.com/////hello

to

domain.com/hello

but from

 domain.com/////héllo

the result is encoded

domain.com/h%25c%25allo

How to prevent accented characters to get encoded when removing multiple slashes after domain ?

EDIT: pear to anubhava's answer

RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*) [NC]
RewriteRule ^ %1/%2 [R=301,L,NE]

The accented character is protected and trimed with succes with more than to repeated slashes

domain.com////////héllo

but not with only 2

domain.com//héllo
Community
  • 1
  • 1
RafaSashi
  • 16,483
  • 8
  • 84
  • 94

2 Answers2

2

This rule should work for you:

RewriteEngine On
RewriteBase /

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

This will do these redirections:

  1. /////help => /help
  2. /////héllo/////abc/////123 => /héllo/abc/123
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • the accented character is protected but the result is domain.com//héllo – RafaSashi Feb 02 '14 at 16:53
  • I have updated the question according to your answer. would it be possible to add a [OR] condition to deal with the double slash? – RafaSashi Feb 02 '14 at 17:06
  • 1
    This will also work for `domain.com/////héllo/////abc/////123` redirecting that to `domain.com/héllo/abc/123` – anubhava Feb 02 '14 at 17:23
  • in fact not for me... testing... true!! I have forgotten the other cleanning rules after thisone. thanks anubhava it is officially the best answer from now – RafaSashi Feb 02 '14 at 17:26
0

anubhava's answer is the shortest but alternatively this one is also working:

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L,NE]
RafaSashi
  • 16,483
  • 8
  • 84
  • 94