0

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.

  • 2
    You'll want the ServerAlias directive, or to set up a separate virtualhost for the non-www version that 301 redirects to the www version (generally better for SEO). – ceejayoz Mar 10 '16 at 19:10
  • I definitely also suggest the redirect approach; there are a lot of reasons to want one single, consistent name for your site, even if you allow people to type different things to get there initially. This is also useful for variants other than with-www-or-not (for example .org and .com or another tld). – mattdm Mar 11 '16 at 12:04
3

You should add a ServerAlias - modify your VirtualHost to look like this:

<VirtualHost *:80>
    # This first-listed virtual host is also the default for *:80
    ServerName www.example.com
    ServerAlias example.com 
    DocumentRoot "/www/domain"
</VirtualHost>

You can also have all subdomains redirect to your root

ServerAlias example.com *.example.com
1

As ceejayoz says in his comment, you need a ServerAlias

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/public_html/www.example.com
</VirtualHost>

Whether you have the ServerName or the ServerAlias as www 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 the UseCanonicalName 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.

0

For Nginx:

  server {

  server_name  www.example.com;

  rewrite ^/(.\*) http://example.com/$1 permanent;

For Apache Example:

  <VirtualHost \*:80>

  ServerName  example.com

  ServerAlias www.example.com

</VirtualHost>

Restart Service after changes:

0

beside your apache question:
www.example.com should be a CNAME for example.com. One A record is enough.

  • If this is true I appreciate it. I was also confused about that when to use CNAME record so I set www and non-www to A record. – user3448600 Mar 11 '16 at 14:15
  • While CNAME is handy, it's not necessary. A works just fine here. – Esa Jokinen May 21 '17 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.