Why some sites require the www prefix while others work just fine without it
Ever wondered why some web sites work if you type their name without the www. prefix, while others give you odd error messages and sometimes even result in the wrong page displaying? This is why.
It all starts with DNS, the Domain Name System. When you type a name in the address bar of a web browser it takes the server name and posts a query to your nearest DNS server. So for instance the URL http://example.com/somepage will result in a DNS query for example.com.
At this point, depending on how the domain was registered, the DNS will either respond with an IP address for example.com, or with a "not found" DNS reply. If there is no IP address record for the name you the browser will display a standard error page telling you that it cannot find such a site.
If the DNS responds back with an IP address, then the browser will attempt to connect and send an HTTP request to it. Here is where things tend to go wrong. HTTP supports a feature called virtual hosting where multiple web sites can be served by the same web server, using just one IP address. In order to distinguish between different sites living on the same server the browser sends an HTTP header indicating which site it wants to talk to. If the web server is not configured to correctly match up your request to a specific website hosted on it, it might return a default page (ISPs and large hosting providers tend to do that) or just serve the wrong website altogether.
If you are a webmaster and for whatever reason you wish to only have your website available when accessed using a www. prefix, you should ensure that your domain name does not have an A record. Most registrars will insert one by default when you first register a domain and so you need to remove it; only add an A record pointing to your web server against the www entry. That's it, now no one will be able to access your site without typing out the full name.
Most people will probably prefer to help their visitors out by allowing access via a shortened name as well. In order for this to work, you need to:
Make sure there is a correct A record against the base domain name
In DNS terms, this is called the @ record - the root of the specific DNS zone. So if your web server has an IP address of 192.168.0.1 the DNS zone should contain the following records:
@ A 192.168.0.1
www A 192.168.0.1
Make sure your virtual hosts are correctly defined
If you are using the popular Apache httpd web server you can use the ServerAlias directive to specify any number of alternative names to your site:
<VirtualHost *:80> ServerName www.example.com ServerAlias example.com w3.example.com ServerAdmin webmaster@example.com DocumentRoot /var/www/example.com </VirtualHost>
Bonus: make sure your web server is set up to serve a default error page if it doesn't recognize the virtual host requested
What happens if someone mistakenly point a DNS name to your web server? In Apache, the first virtual host is the default one and if no server name or alias matches the request, that is what will get served. So if you are hosting multiple virtual hosts on your server, make sure the first virtual host you define points to a page containing some meaningful message stating that the site is not yet up.
Optional: rewrite the URLs to redirect visitors to your preferred domain name
If you would prefer your visitors to use one specific name for your site - e.g. for marketing reasons - you can use the following configuration to redirect those using the alternative name:
<VirtualHost *:80> ServerName www.example.com Redirect permanent / http://example.com/ </VirtualHost> <VirtualHost *:80> ServerName example.com DocumentRoot /var/www/example.com </VirtualHost>
In this case you should remove www.example.com from the aliases.
Bottom line: make sure that whatever DNS records you do have are also configured in your web server. And unless you have a good reason not to, you should probably add the short domain name as a valid alternative.