Aug 182010
 

From domainhelp.com

How do I point “www.domain.com” at “domain.com” or vice-versa?

You may have your webserver setup for either www.example.com or just example.com and you want it to work either way, whether people type in the “www” part of your domain or not. (By way of info, “www” is purely a convention, it isn’t in itself necessary to “make your domain work” over the web).

There are a couple ways to do this:

Setup a host record or A record for the root example.com domain. Then create a CNAME record or alias which points www.example.com at this host record.

In a bind style zone file you would put this:

example.com.                   IN A    192.168.3.44
www.example.com.                 IN CNAME example.com.

Take special care to make the root domain the host/A record and the “www” hostname the alias/CNAME. If you do it the other way around, more often than not, you will cause your zone to fail with errors because under most circumstances, the root of your zone cannot itself be an alias/CNAME.

Alternatively, you could simply setup a host record for each record, both pointing to the IP address:

example.com.                   IN A    192.168.3.44
www.example.com.                 IN A 192.168.3.44

The first method has a slight advantage in that you only need to change one record if the IP address of your webserver changes. If you are using an outsourced DNS host to do this, the interface usually calls the A records “hosts” and the CNAME records “aliases”. Often people get this far and then their website loads fine under one method (say, http://example.com) but they get unexpected results from the other http://www.example.com, such as an error “No website configured at this address” or getting some other website entirely. This is because you still need to tell your webserver to respond to both hostnames.

When a user accesses a website in her browser, the browser sends a header setting an environment variable called HTTP_HOST, and webservers key off of this value to decide which local “webhost” or “virtualhost” to load (this is how web hosting companies can host thousands of websites on a single IP address.) In our example, we’re using the apache webserver, and you need to specify in your “VirtualHost” directive that the website needs t respond to requests from both example.com and www.example.com, you do this with a “ServerAlias” statement:

<virtualhost 192.168.3.44>

ServerName              example.com

ServerAlias              www.example.com *.example.com

# other stuff

</virtualhost>

You can add multiple entries per ServerAlias line, in our example above we have added a “wildcard” entry for example.com, if you setup a wildcard DNS entry in the zonefile, this webserver would load properly for http://.example.com. You could also enter multiple ServerAlias lines, if you wanted your website to respond on another domain name as well.

Share This!
 Posted by at 1:30 am

Sorry, the comment form is closed at this time.