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.



