Using URL within Vhost container with mod_perl dynamically

We want to retrieve URL and use it inside our vhosts, realtime.

To demonstrate my case, I placed random numbers on ServerAdmin directive,

$ServerAdmin = int(rand(100))."loko\@koko.com";

As you may know, this random number is assigned only when you restart the server, and never changes until another restart.

This is what we want: if julie.myperl.com is the requested domain, then $ServerAdmin = [email protected], that is, realtime.

Hopefully there is a solution, since this will help us remove thousands of config files, and save us from thousands of apache reloads.

Thanks, Devrim

here is kind of similar situation and solution,

http://httpd.apache.org/docs/1.3/mod/mod_vhost_alias.html

and here is why ask this question, we have built an API in which subdomains signify unix account names, so variable part of {$user}.domain.com must be used within vhost conf. Using variables within Apache virtualhost containers


Solution 1:

I answered a similar question a while back that may help you. Basically it's a bit of perl that will do any sort of dynamic configuration based on the url. In that example it was setting the Require directive based on the directory, but with a tiny bit of adjustment it should do what you need as well.

Take a look here: Dynamic authentication realms in Apache

Hope that helps.

Edit: This should to do it. I used server_admin to manipulate the server admin address directly rather than add_config because add_config acts like a <Directory> block, and ServerAdmin isn't allowed there.

<Perl>
 use Apache2::ServerRec qw//;
 use Apache2::ServerUtil qw//;
 use Apache2::RequestRec qw//;
 use Apache2::RequestUtil qw//;
 use Apache2::Const qw/OK DECLINED/;

 my $s = Apache2::ServerUtil->server;

 $s->push_handlers(PerlHeaderParserHandler => sub { my($r) = @_;
  if ( $r->hostname =~ m/(.*)\.([^.]+\.\w+)$/ ) {
   my($subdomain,$domain) = ($1,$2);

   eval{
    $r->server->server_admin("$subdomain\@$domain");
   };
   if ( $@ ) { warn $@ }

   return OK;

  } else {
   return DECLINED;
  }
 });
</Perl>