So I've got this site that's being served as a virtual host by Apache 2. And it has a metric ton of alternative domain names that are all supposed to resolve to it.

Up to now I've handled things like this by adding the extra domains in the ServerAlias list in the vhost configuration file. But as the number of aliased domains has grown longer, that list has gotten extremely long and unwieldy. It also requires adding both the bare domain and the 'www' version separately, which (since I have to remember to add both) leaves room for me to be an idiot and forget to add one or the other. Hasn't happened yet, but when the list is long enough omissions like that don't leap out at you the way they do when there's only a couple of domains there, so I worry it's inevitable someday.

It seems like there ought to be a way to kick the list of aliases out of the vhost configuration file to some other place where they can be managed more easily. Even a text file with each alias on its own line would be an improvement, since at least then it'd be easier to visually scan the list than it is with a long comma-separated line. And it seems like there should be a way to have the 'www' subdomain automatically handled for the vhost as well, rather than needing to manually add a 'www' alias for each domain. But I've been looking for things like this and haven't found any obvious solutions yet.

That's probably just me not looking in the right place, but just in case -- is there a more elegant way to handle ServerAliases for a vhost? And if so, what is it?


You can include arbitrary configuration settings from an external file using the Include directive in place of the actual option you want to pull in from a file. (there is also a mod_perl option, if you are an old-skool perl wizard ;-)

1. use the Include directive, to include a list of ServerAlias from a text file in conf.d/mydomain.conf like so;

<VirtualHost *:80>
    ServerName www.mydomainname.com 
    # instead of ServerAlias, include a file containing those directives
    Include /etc/httpd/conf/MyDomainServerAlias.list

Then generate the list with the ServerAlias directive prefixed like so into

ServerAlias mysqdomain.com www2.mysqdomain.com www.mysqdomain.com www.mysqdomain.com www.mysqdomain.com www.mysqdTTTomain.com www.mCCysqdomain.com www.mysqdomain.com

or one per line, as that might be more convenient for inspection and scripting;

ServerAlias mysqdomain.com 
ServerAlias www.mysqdomain2.com 
ServerAlias mysqdomain2.com 
ServerAlias www.mysqdomain.com

2. you can dynamically configure apache using <Perl> blocks using mod_perl

  $ServerName = `hostname`;
  $ServerAlias = 'somethingelse.com';

Hence you can use whatever Perl wizardry you like to configure a list of Aliases at startup ;-)

3. probably some other (simpler ;-) ways...

  • Great answer, even if my Perl wizardry is pretty rusty these days :D Thanks! – jalefkowit May 13 '12 at 3:33

You can repeat the ServerAlias line in your vhost config file multiple times. I think closer to an dumb text.file you won't get. However, you have to remember to add the www one...


What if you configure your "catch-all" site at main context, ouside of scope? Then you'll need ServerAlias only for domains which are to be distinct virtual sites.


How about writing a program to fill in those lines for you? In a similar application I have a VHost per name and about 50 names to take care of and I have Puppet generate them. I then just list:

mySite::newSite{ 'example.com': }

And it generates two VHosts (one for port 80, one for port 443) with example.com and www.example.com. It means having a file full of those mySite::newSite lines, but that's less bad :)

You don't have to use Puppet, any language could help with this. Ruby erb files are an option.

  • or chef ... ;-) – Tom May 11 '12 at 22:11
  • Impossible! Chef could never do such a complex task! :D – Bill Weiss May 11 '12 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.