> RewriteMap

For a description of the syntax, see [WWW]Apache manual

Problem:

We want to map
www.example.com --> /home/www/htdocs/hosts/www/
   the hostname www may be omitted from now on
www.sub1.example.com --> /home/www/htdocs/hosts/sub1/
www.sub2.sub1.example.com --> /home/www/htdocs/hosts/sub1/sub2/
www.sub3.sub2.sub1.example.com --> /home/www/htdocs/hosts/sub1/sub2/sub3/
etc.

Recipe:

RewriteEngine on
RewriteMap sub /physical/path/to/rwmap.pl
RewriteLock  /var/lock/map.sub.lock
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z.-]+)\.example\.com
RewriteRule ^/(.*) /home/www/htdocs/hosts/${sub:%2}$1 [L]
rwmap.pl:
#!/usr/bin/perl

$| = 1;

while(<STDIN>)
{
chomp($_);
my @lables = split(/\./);
@lables = reverse @lables;
foreach my $label (@lables) {
        $path .= $label . "/";
        }
print $path . "\n";
}

Discussion:

Someone may write a brief description of the ruleset above in the near future.

External 1:1 Rewriting Program in Python in Perl

(Untested!)
  1 
  2 
  3 
  4 
#!/usr/bin/env python
import sys
while 1:
  sys.stdout.write(sys.stdin.readline())