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

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)
  • 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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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