1

i try to catch multiple slashes at the ende or inside of url, but not those slashes (which could be two or more), which are placed after protocol (http://, https://, ftp:// file:///)

i tried many findings in similar SO-threads, like ^(.*)//+(.*)$ or [^:](\/{2,}), or ^(.*?)(/{2,})(.*)$ in http://www.regexr.com/, https://regex101.com and http://www.regextester.com/. But nothing worked clear for me.

Its pretty weird, that i can't find a working example - this goal isn't such rar. Could somebody share a working regex?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Evgeniy
  • 2,337
  • 2
  • 28
  • 68

1 Answers1

3

Here is a rule that you can use in your site root .htaccess to strip out multiple slashes anywhere from input URLs:

RewriteEngine On

RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]

THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1.

Pattern inside RewriteRule automatically converts multiple slashes into single one.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • does it work only on the url's end? your rule for multiple slashes in the middle of url has other look: http://stackoverflow.com/a/21513277/1992004 – Evgeniy Jul 19 '16 at 11:47