Forum Moderators: phranque

Message Too Old, No Replies

Removing extra trailing slashes

         

Excalibur

6:20 am on May 27, 2012 (gmt 0)

10+ Year Member



Hello. I am trying to remove extra trailing slashes when using mod_rewrite. I found a piece of code that almost works, it just when you have over 12 slashes, it does not remove them all right away. For example, domain.com///////////////////foooooooooooo///////////////baaaaaaaaaaaaaaaaaaaar////////////// will take a few reloads to strip all the extra slashes.

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

Is there a way to modify this to make it simply detect any number of extra slashes?

g1smd

6:45 am on May 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't try to fix this in htaccess. It's not the right tool for the job. Use PHP to fix the URL, after rewriting (that's rewrite not redirect) the request to a special script.

RewriteRule (^/|//) /fixme.php [L]


Unusually you will place this rewrite BEFORE your non-www to www redirect but you MUST add
RewriteCond !^/fixme.php
as an extra condition in your non-www to www redirect in order to not expose fixme.php as a new URL back out on to the web when a non-www URL with multiple slashes is requested.

In fixme.php detect the requested URL, and use the PHP preg_replace function to replace multiple slashes with single slashes. Next, trim any and all slashes from the beginning of the string. Finally, add the hostname back on and use HEADER to send a 301 redirect to this new URL.

Excalibur

7:16 am on May 27, 2012 (gmt 0)

10+ Year Member



This is an interesting way of doing it, also makes sense.

Thanks! I will work on it :)

g1smd

7:32 am on May 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm using a similar setup on a site that had 40 000 duplicate content URLs for 800 pages of content and which has moved to a new extensionless URL format.

In this case the special PHP script is used to redirect URL requests with parameters, old style friendly URLs, and a myriad of broken URL formats all to the new friendly URL format that the site now uses.

wilderness

1:56 pm on May 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A thread from early April.

Double slashes [webmasterworld.com]

The title should actually read "multiple slashes".

I've had the first line in place for nearly two months and it resolves everything two directories deep (actually root+one), however does nothing for directories deeper than two (actually root+one).

g1smd

4:02 pm on May 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code I suggested above will work for any level of URL screwup you care to throw at it. :)

wilderness

4:36 pm on May 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



g1smf,
Unfortunately, the only PHP that I'm using on my own accord, is a submission form.

g1smd

3:07 pm on May 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Typo in msg #4458310:

RewriteCond !^/fixme.php


should read:

RewriteCond %{REQUEST_URI} !^/fixme.php