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

  • Posted September 9, 2014

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. and 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 I get 301 redirected to If I disable the site using a2dissite, then 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’:


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.


Ye, we are going to need to see your vhost files before we can 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.

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.


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.


– rustx