On Mon, May 07, 2001 at 05:35:57PM -0700, Jonathan Hilgeman wrote:
> Hi,
> I've been trying to do this for some time but can't figure out how.
> Basically I want a setup where anyone at www.UserName.domain.com or
> UserName.domain.com will have their DocumentRoot set to
> /www/httpd/html/UserName. So far I've tried this:
> 
> <VirtualHost 111.111.111.111>
>       UseCanonicalName Off
>       <Perl>
> 
>               if($HTTP_HOST =~ s/(?:www\.)?(.*)\.domain\.com//g)
>               {
>                       $VirtualDocumentRoot = "/www/httpd/html/$1";
>               }
>               else
>               {
>                       $VirtualDocumentRoot = '/www/httpd/html/%-3';
>               }
>               
>               1;
> 
>       </Perl>
> 
> </VirtualHost>
> 
> The second $VirtualDocumentRoot line works fine, but I NEED to be able to
> get the first line to work instead, using <Perl> if possible.
> 
> Any guesses?

Your problem is that this code is evaluated at startup-time, not request-time.
So this means it will be run once globally, the regexp won't match and you just
configure the default.  There are many ways to do it.

First, look at mod_vhost_alias http://httpd.apache.org/docs/mod/mod_vhost_alias.html.
        VirtualDocumentRoot     /www/httpd/html/%0

Would map requests to www.username.com to /www/httpd/html/www.username.com
And for the username.com, you could simply use a symlink.  

Second, use mod_rewrite http://httpd.apache.org/docs/mod/mod_rewrite.html
Something like :

      RewriteEngine on
      RewriteCond   %{HTTP_HOST}                 ^(www\.)?[^.]+\.host\.com$
      RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
      RewriteRule   ^(www\.)?([^.]+)\.host\.com(.*) /www/httpd/html/$2$3        

Third, you could do it in mod_perl with a custom handler

<Perl>
use Apache::Constants qw(:common);
sub My::MassVHosting::handler {
        my $r = shift
        if($r->header_in('Host') =~ /^(www\.)?([^.]+)\.domain,com/)
                {
                $r->filename("/home/httpd/html/$2");
                $r->stat();
                return OK;      
                }
        return Apache::Constants:;DECLINED;
        }
</Perl>
PerlTrancHandler My;;MassVHosting

For more information about mod_per magic, consider getting the book or reading the 
guide
http://perl.apache.org/guide/

Hope this helps.

P.S. I am not responsible for tyops ;-)
> Jonathan
> 
> 

-- 

Reply via email to