Mobile app version of vmapp.org
Login or Join
Gonzalez347

: Remove multiple trailing slashes in a single 301 in .htaccess? There is a similar question here, but the solution does not work in Apache for our site. I'm trying to remove multiple trailing

@Gonzalez347

Posted in: #301Redirect #Apache #Htaccess #TrailingSlash

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:
www.mysite.com/category/accessories//// becomes www.mysite.com/category/accessories/

However, it redirects once for every extra slash. So:
www.mysite.com/category/accessories/////// 301 Redirects to www.mysite.com/category/accessories////// 301 Redirects to www.mysite.com/category/accessories///// 301 Redirects to www.mysite.com/category/accessories//// 301 Redirects to www.mysite.com/category/accessories/// 301 Redirects to www.mysite.com/category/accessories// 301 Redirects to 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:
www.mysite.com///// does not redirect but it should.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Gonzalez347

2 Comments

Sorted by latest first Latest Oldest Best

 

@Annie201

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_HOST}/%1 [R=301,L]

10% popularity Vote Up Vote Down


 

@Mendez628

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

RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . / [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme