Remove multiple trailing slashes in a single 301 in .htaccess?

Posted by Jakobud on Pro Webmasters See other posts from Pro Webmasters or by Jakobud
Published on 2012-10-19T19:58:00Z Indexed on 2012/10/19 23:22 UTC
Read the original article Hit count: 474

Filed under:
|
|

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:

http://www.mysite.com/category/accessories////
becomes
http://www.mysite.com/category/accessories/

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

http://www.mysite.com/category/accessories///////
301 Redirects to
http://www.mysite.com/category/accessories//////
301 Redirects to
http://www.mysite.com/category/accessories/////
301 Redirects to
http://www.mysite.com/category/accessories////
301 Redirects to
http://www.mysite.com/category/accessories///
301 Redirects to
http://www.mysite.com/category/accessories//
301 Redirects to
http://www.mysite.com/category/accessories/

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.

© Pro Webmasters or respective owner

Related posts about apache

Related posts about htaccess