httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Torsten Foertsch <>
Subject Re: [users@httpd] Redirecting paths with extra slashes
Date Sat, 08 Dec 2007 14:40:09 GMT
On Sat 08 Dec 2007, Christian Lerrahn wrote:
> > RewriteEngine On
> > RewriteRule (.*)//+(.*) $1$2 [R=permanent,L]
> Thanks for that. I'm sorry to still bother. I'd like to get rid of
> paths like //foo/bar, too, which do not match with this rule. To be
> honest I don't quite understand the rule. That's probably the reason
> why I can't modify if correctly to match to //foo/bar as well. When I
> saw the regexp, I thought that I would end up without any slashes but
> obviously I'm not. Wouldn't matching /foo//bar/ match as $1=/foo and
> $2=bar/ ? Why does it not match like that? Then also it seems to me
> that (.*) should also match an empty string which would mean that
> leading slashes would get stripped, too. Why does that not happen?

You need to know that * in regexes is greedy. That means it eats up as many 
characters as it could to match the regexp. So in /foo///bar $1 gets /foo/ 
and not only /foo.

What you need for $1 is a nongreedy one (*? instead of *), something like 

RewriteRule (.*?)//+(.*) $1/$2 ...

You can try this in a little Perl-onliner:

perl -ne 'BEGIN {$|=1; print "> "} if(m!(.*?)//+(.*)!) {print "$1\t$2\n"} else 
{print "no match\n"} print "> "'

It offers you a "> " prompt to enter a string that is matched against that 
regexp. Then $1 and $2 are printed delimited by a tab-character.

You'll see that the new regexp matches even at the beginning of the line:

> /foo/bar
no match
> /foo//bar
/foo    bar
> /foo///bar
/foo    bar
> ///foo///bar
> //foo//bar

The last 2 of the examples above reveal another problem with the approach. The 
RewriteRule matches only the first occurrence and then sends a redirect to 
the browser. If your URL contains multiple occurrences of subsequent slashes 
you may hit the browser's redirect limit.

To overcome that you can try to loop in mod_rewrite (untested):

RewriteRule (.*?)//+(.*) $1/$2 [E=R:$1/$2,N]

RewriteCond %{ENV:R} .
RewriteRule . %{ENV:R} [R=permanent,L]


A: It reverses the normal flow of conversation.
Q: What's wrong with top-posting?
A: Top-posting.
Q: What's the biggest scourge on plain text email discussions?

The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:> for more info.
To unsubscribe, e-mail:
   "   from the digest:
For additional commands, e-mail:

View raw message