Since RewriteRule starts with the path (so we don't need to worry about the domain), what you can say is that you want to remove all slashes except for the first one, or, remove a slash when not preceded by the start of string.
Which in regex means using a negative lookbehind, like this:
(?<!^)/
Where the (?<!
...)
is the negative lookbehind syntax and ^
means start of string.
So in the config it'll look like:
RewriteRule (?<!^)/ $1 [L,R=301]
One thing here which is a bit of a guess is the $1
to represent empty string - I've not done this with mod_rewrite before, and I couldn't find anything explaining if there is a "correct" way to replace with empty string, but in other regex engines, using an unused back reference will result in empty string, so should be fine. (If not, I'd see if ""
worked instead.)
Oh, and the [
...]
part is a comma-delimited list of rewrite options. The L
is for Last - saying don't process further rules (usually this is desired behaviour) and the R=301
is a 301 (permanent) redirect (instructing spiders to update their records with the new URL). More details on the rewrite options here.