Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using this rule to remove all trailing slashes:

RewriteRule ^(.*)/////////$ $1 [N]
RewriteRule ^(.*)////////$ $1 [N]
RewriteRule ^(.*)///////$ $1 [N]
RewriteRule ^(.*)//////$ $1 [N]
RewriteRule ^(.*)/////$ $1 [N]
RewriteRule ^(.*)////$ $1 [N]
RewriteRule ^(.*)///$ $1 [N]
RewriteRule ^(.*)//$ $1 [N]
RewriteRule ^(.*)/$ $1 [L,R=301]

But the 301 header does only get sent when the last rule is hit. I don't get these rules combined and working.

This RewriteRule ^(.*)/+$ $1 [R=301,L] does NOT work, it looks like it is but it generates a rewrite loop. I need all slashes removed at once...

share|improve this question

1 Answer 1

I would rather fix the source of the extra slashes than use this kind of redirect-kludge to avoid problems with it.

However, this is the rule that does what you want:

RewriteRule ^([^/]*)/+$ $1 [L,R=301]

Instead of using .* (0 or more instances of any character, use "any character but /" character class. This way the (.*) part won't match any / part of the original URL, causing the redirect loop.

share|improve this answer
    
If the URL is http://example.com/test/page/site/////////////////, does this still match the / in the middle of the url? –  DanFromGermany Feb 27 at 11:58
    
True, it won't work for that URL. Can't you fix the piece of software that generates the invalid URLs? –  Tero Kilkanen Feb 27 at 17:27
    
There is no invalid URL, this rule is used to prevent and capture wrong generated urls by 3rd party companies. –  DanFromGermany Feb 27 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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