9

how to remove multiple slashes in URI with 'PREG' or 'HTACCESS'

site.com/edition/new/// -> site.com/edition/new/


site.com/edition///new/ -> site.com/edition/new/

thanks

Lelis
  • 325
  • 1
  • 3
  • 11

5 Answers5

25
$url = 'http://www.abc.com/def/git//ss';
$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url);
// output http://www.abc.com/def/git/ss

$url = 'https://www.abc.com/def/git//ss';
$url = preg_replace('/([^:])(\/{2,})/', '$1/', $url);
// output https://www.abc.com/def/git/ss
Dos
  • 771
  • 9
  • 23
  • Nice answer, Could you please explain how this regex works? – Gihan Dec 15 '15 at 06:05
  • On the tool linked by @ツLiverbool paste `([^:])(\/{2,})` as the regex, then paste $url as test string or any other url you want to test – briankip Oct 20 '17 at 06:54
  • @Gilhan The sequence [...] means match any of these chars. In this case, [^:] means match any char except a colon. The leading ^ negates the sequence. This sequence is surrounded by parentheses which makes it a 'capturing group' and the value captured can be used in the substitution as $1 The sequence [\/]{2,} means look for two or more forward slashes. Because the whole expression is delimited by forward slashes the forward slash being matched must be escaped which is what the backslash does. This sequence is surrounded by parentheses so it is also captured but it is not necessary. – bseddon Dec 06 '19 at 10:27
  • @Gilhan Note that the example provided in the answer does not work if there is more than one sequence of forward slashes such as: https://www.example.com/def//git//ss as the expression will match the first instance. To address this, it should use the 'greedy' directive: preg_replace('/([^:])\/{2,}/g', '/') – bseddon Dec 06 '19 at 10:29
15

using the plus symbol + in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them

   $url =  "site.com/edition/new///";

$newUrl = preg_replace('/(\/+)/','/',$url);

// now it should be replace with the correct single forward slash
echo $newUrl
Ibu
  • 39,552
  • 10
  • 71
  • 99
  • Great idea but how can I to do the checking after 'edition' As this example $ url = "site.com/edition///new///"; $ newUrl = preg_replace ('/edition(\/+)/','/',$ url); I do not know apply – Lelis Jun 14 '11 at 05:51
0

Simple, check this example :

$url ="http://portal.lojav1.local//Settings////messages";
echo str_replace(':/','://', trim(preg_replace('/\/+/', '/', $url), '/'));

output :

http://portal.lojav1.local/Settings/messages
Bruno Ribeiro
  • 1,044
  • 13
  • 21
0

Edit: Ha I read this question as "without preg" oh well :3

function removeabunchofslashes($url){
  $explode = explode('://',$url);
  while(strpos($explode[1],'//'))
    $explode[1] = str_replace('//','/',$explode[1]);
  return implode('://',$explode);
}

echo removeabunchofslashes('http://www.site.com/edition////new///');
Scuzzy
  • 11,272
  • 1
  • 40
  • 42
0

http://domain.com/test/test/ > http://domain.com/test/test

# Strip trailing slash(es) from uri
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)[/]+$ $1 [NC,R,L]

http://domain.com//test//test// > http://domain.com/test/test/

# Merge multiple slashes in uri
RewriteCond %{THE_REQUEST} ^[A-Z]+\ //*(.+)//+(.*)\ HTTP
RewriteRule ^ /%1/%2 [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ //+(.*)\ HTTP
RewriteRule ^ /%1 [R,L]

Change R to R=301 if everything works fine after testing...

Does anyone know how to preserve double slashes in query using above method?

(For example: /test//test//?test=test//test > /test/test/?test=test//test)

Mike
  • 1,597
  • 3
  • 21
  • 16