Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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\";

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 is the requested domain, then $ServerAdmin =, 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,

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

share|improve this question
up vote 1 down vote accepted

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:

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.

 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);

   if ( $@ ) { warn $@ }

   return OK;

  } else {
   return DECLINED;
share|improve this answer
Hi Jeff, this is exactly what I am looking for, and it's very very important. I couldn't change your script, could you help me out? I just need {} part of any {} to be assigned to $1, I can do the rest. I'd really really appreciate your 5 mins on this! Thanks! – Devrim Nov 19 '09 at 3:48
Answer updated. Took a bit more than five minutes, but it was fun to mess with. ;-) Let me know if that does what you need. – Jeff Snider Nov 19 '09 at 5:05
Thanks Jeff! Awesome! Please drop me a note at devrim at if you are up to some consulting, we need these perl hacks for perfect configuration... Thanks again! – Devrim Nov 19 '09 at 17:06
I actually am having difficulty adjusting this regex.. I think Perl using slightly different syntax. I have to push this variable to <IfModule mpm_itk_module> AssignUserId $subdomain $subdomain </IfModule> . I was able to do this before: $IfModule{"mpm_itk_module"} = {AssignUserId => "devrim devrim"}; now this didn't work within your snippet.. – Devrim Nov 19 '09 at 17:11
$r->add_config(["AssignUserId $subdomain $subdomain"]); may work. Please try it. If that doesn't work, you'll probably have to approach this problem from another angle. – Jeff Snider Nov 19 '09 at 22:24

Your Answer


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.