Will a virtual host with 'ServerName example.com' catch sub-domains as well as the root domain?

September 9, 2014 1.8k views

I'm trying to run a new website from a sub-domain on my droplet, as well as a website at the root of the domain, e.g. example.com and new.example.com serving from different directories.

I've set up the directories, created virtual host files for each site, and enabled both sites.

Currently, every time I visit new.example.com I get 301 redirected to example.com. If I disable the example.com site using a2dissite, then new.example.com loads as expected.

The only reason I can think of this happening (I've investigated .htaccess files and ruled that out) is if the following line were catching the sub-domain because it doesn't contain 'www':

'ServerName example.com'

Is this possible, or is the problem likely somewhere else and if so what could it be? I've exhausted my Google searches : /

I would like my main site to continue to serve without 'www' if possible, whilst also running the new sub-domain on the same droplet.

Thanks for any help.

  • Hi! If you could edit your question to actually contain the VirtualHosts or put them on a pastebin, it would help us figure out your issue.

  • Ye, we are going to need to see your vhost files before we can help.

1 Answer


I think I can provide you a few information to solve that issue with Apache Named VirtualHosts.

When you configure Apache with VirtualHosts, it keeps serving the first virtualhost that was enabled as the "default Virtualhost". Unless you declare a ServerAlias directive, Apache will not catch any subdomain more than the one declared in ServerName. At the end, if Apache doesn''t know the ServerName asked for, it returns the first virtualhost that was activated.

In your case, I think you should check that both your VirtualHosts contains a ServerName directive, and you should ensure that your VirtualHost declaration starts with <VirtualHost *:80>, to be sure you can handle multiple domain names on the same port.

You should also check the /etc/apache2/ports.conf file, to be sure it contains a NameVirtualHost *:80 line. If you have all this set, then you can restart Apache2, and your webserver will serve 2 websties accordingly to their domain names.

Hope this could help.



Have another answer? Share your knowledge.