2

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?

jalefkowit
  • 123
  • 1
  • 4

4 Answers4

2

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
...
</VirtualHost>

Then generate the list with the ServerAlias directive prefixed like so into
/etc/httpd/conf/MyDomainServerAlias.list;

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

 <Perl>
  $ServerName = `hostname`;
  $ServerAlias = 'somethingelse.com';
  </Perl>

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

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

Steve
  • 282
  • 1
  • 6
Tom
  • 10,886
  • 5
  • 39
  • 62
0

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...

ercpe
  • 566
  • 3
  • 15
0

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.

yrk
  • 2,347
  • 16
  • 22
0

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.

Bill Weiss
  • 10,782
  • 3
  • 37
  • 65