Home / Blog / Web Development

Web Development

Fix: Wrong Virtual Host Opens When You Visit a Domain Without WWW

If your domain opens a different site when you leave out "www", your virtual host entries are missing a ServerAlias.

In this article
  1. Scenario
  2. Why it happens
  3. The solution
  4. Apply and test
  5. Frequently asked questions

If a website loads different content when you type its domain without "www", the cause is almost always your Apache virtual host entries. We set up a virtual host for one of our sites and found that the non-www address opened the first virtual host instead.

Key takeaways

  • Apache falls back to the first virtual host when no ServerName or ServerAlias matches.
  • Set ServerName to the main domain and ServerAlias for other forms.
  • Test with apachectl configtest, then reload Apache.

Example scenario

Two virtual hosts on one server, each with its own document root and a www ServerName:

<VirtualHost *:80>
    DocumentRoot /www/example1
    ServerName www.example.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /www/example2
    ServerName www.example2.com
</VirtualHost>

Why the wrong site loads

Opening example2.com without www showed the content of example.com. Apache looks for a matching ServerName or ServerAlias. When none matches, it falls back to the first virtual host in the configuration, which here was example.com.

The solution: ServerName and ServerAlias

  • ServerName: the primary domain, such as example2.com.
  • ServerAlias: another form of the name, such as www.example2.com or *.example2.com.
<VirtualHost *:80>
    DocumentRoot /www/example2
    ServerName example2.com
    ServerAlias www.example2.com
</VirtualHost>

With both set, requests for example2.com and www.example2.com reach the correct document root.

Apply and test

sudo apachectl configtest
sudo systemctl reload apache2

For SEO, also pick one preferred version (with or without www) and 301-redirect the other to it, so search engines see one site. Need server help? See our hosting and server support.

Frequently asked questions

Why does my domain open another website?

No virtual host matched the requested name, so Apache served the first one defined.

Should I redirect www to non-www?

Pick one version and redirect the other with a 301 redirect, so search engines treat them as one site.

The Ucodice Team is a group of passionate developers, designers and strategists delivering web, mobile and software solutions to clients worldwide.

Ready to take the next step?

Explore our web development services or tell us about your project. A senior member of our team will get back to you with clear next steps.

Let's start your project

Tell us what you want to build or grow. We'll reply within one business day with clear next steps.

Phone: 9560430495  ·  Email: contact@ucodice.com

Office: Ucodice Technologies, IHDP Business Park, Street Number 7, Sector 127, Noida, Uttar Pradesh 201304. Also serving Delhi NCR and clients worldwide.

Trusted and certified

  • Google PartnerCertified for Google Ads
  • Meta business PartnerCertified for Meta Ads
  • 4.3 / 5 on Google47 Google reviews
  • 4.9 / 5 on Freelancer.in245 reviews

Free consultation

Tell us about your project.

Get a free quote