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

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.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


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