Removing duplicate slashes

Discuss practical ways rearrange URLs using mod_rewrite.

Removing duplicate slashes

Postby DanielStutzbach » Wed Aug 19, 2009 6:56 am

I'm attempting to use mod_rewrite to remove duplicate slashes from URLs. The following rule works for me in most cases:

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

However, it doesn't work on a URL such as the following:

Using RewriteLog, I see the following "init rewrite engine with requested uri /foo". It appears that Apache only passes a single leading slash to the rewrite rules, regardless of how many leading slashes are used in the original URI. Is there some way I can work around this?

I'm running Apache 2.2.9. Any insights greatly appreciated.
Posts: 6
Joined: Wed Aug 19, 2009 6:49 am

Postby richardk » Fri Aug 21, 2009 5:52 am

They're probably already cleaned up internally by Apache. Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{THE_REQUEST} \ (.*)//+(.*)\  [NC]
RewriteRule .* %1/%2 [R=301,L]
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby DanielStutzbach » Fri Sep 11, 2009 2:57 am

Thanks! That did the job.
Posts: 6
Joined: Wed Aug 19, 2009 6:49 am

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest