There is a similar question here, but the solution does not work in Apache for our site.

I'm trying to remove multiple trailing slashes from URLs on our site. I found some .htaccess code that seems to work:

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

This rule removes multiple slashes from anywhere in the URL:


However, it redirects once for every extra slash. So:

301 Redirects to
301 Redirects to
301 Redirects to
301 Redirects to
301 Redirects to
301 Redirects to

Is it possible to rewrite this rule so that it does it all in a single 301 redirect?

Also, this above directive does not work at the root level of our site:

http://www.mysite.com///// does not redirect but it should.

2 Answers 2


If the slashes may only occur at the end of the URL, you may use this

RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . $1/ [R=301,L]
  • 1
    I think you mean %1 (back reference to the RewriteCond pattern) in your RewriteRule, rather than $1? As it stands you will always be redirected back to the document root. Also, I'm curious as to whether (?:/) is required in the RewriteCond, can you not simply use /?
    – MrWhite
    Commented Oct 20, 2012 at 15:49
  • yes, sorry force of habit in that syntax. The $1 is ok, but you can omit the (?: ) and use only /
    – PatomaS
    Commented Oct 21, 2012 at 0:18
RewriteEngine on
RewriteBase /
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]
  • 1
    Can you explain a little bit about how this works? A code only answer is only helpful with some exlaination. Commented Jan 24, 2015 at 15:45
  • It's exactly what I need and the only one it works for me. Perfect!
    – Unix
    Commented Dec 8, 2019 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.