I have a domain - example.com
.
Below are my DNS records.
example.com A IP address
www.example.com A IP address
Here is my Apache virtual host configuration.
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/public_html/www.example.com
</VirtualHost>
Now when I type example.com
(without www
part) in my browser I get default Apache page not file from DocumentRoot
. When I add www
to my domain everything is OK.
Now the question is how to set this up so that both domains www.example.com
and example.com
serve the same file from document root? Should I handle this at DNS level or configure web server somehow? I am noob in all this so the question is probably stupid.
You should add a ServerAlias - modify your VirtualHost to look like this:
You can also have all subdomains redirect to your root
As ceejayoz says in his comment, you need a
ServerAlias
Whether you have the
ServerName
or theServerAlias
aswww
is really a matter of preference. It can make a difference if you subscribe to the no-www belief or the yes-www belief, as if you are using theUseCanonicalName
directive, you might find Apache redirecting your users to somewhere slightly unexpected.Also as ceejayoz mentioned, you want to choose a single camp (www.example.com or just naked example.com) and stick with it. You don't really want to be serving the same content on both variations - the alias'd one should really redirect to the canonical one. There are a thousand sites out there that will tell you how to set this up with
mod_rewrite
.For Nginx:
For Apache Example:
Restart Service after changes:
beside your apache question:
www.example.com should be a CNAME for example.com. One A record is enough.