Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

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.