Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to replace some forward slashes (/) of a URL to dots (.) in a RewriteRule? It doesn't have to be done exclusively with a RewriteRule, but definitely not with a script.

Example 1:

INPUT:  /document/my/document.html
OUTPUT: /document-my.document.html

Example 2:

INPUT:  /document/depth/of/path/can/vary.html
OUTPUT: /document-depth.of.path.can.vary.html
share|improve this question
up vote 4 down vote accepted

I think you could do this, using an iterative approach. The "variable number of replacements" means you must use the same rule several times, one for each "/" to replace.

Try this:

RewriteRule ^/([^/]+)/(.*)$ /$1.$2 [N]

Some details:

  • The pattern matches / + anything + / + anything
  • You need to match the first /explicitely, because it will always be there and mustn't be replaced with a .
  • The [N] flag means: Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. (from the Apache mod_rewrite documentation)
share|improve this answer
Thanks, that's exactly what I was looking for! I've just added a few minor tweaks (above) to complete it. – Yuriy Nemtsov Mar 15 '10 at 8:04

What worked (thanks to Jonathan Clarke's answer):

RewriteCond %{REQUEST_URI} ^/document.*
RewriteRule ^/([^/]+)/(.*)$ /$1.$2 [N]
RewriteRule ^/document\.(.*)\.html /document-$1 [L]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.