What’s the point in having “www” in a URL?

Other than for historical reasons, is there is reason to have “www” in a URL?

Should I create a permanent redirect from www.xyz.com to xyz.com, or from xyz.com to www.xyz.com? Which one would you suggest and why?


Solution 1:

One of the reasons why you need www or some other subdomain has to do with a quirk of DNS and the CNAME record.

Suppose for the purposes of this example that you are running a big site and contract out hosting to a CDN (Content Distribution Network) such as Akamai. What you typically do is set up the DNS record for your site as a CNAME to some akamai.com address. This gives the CDN the opportunity to supply an IP address that is close to the browser (in geographic or network terms). If you used an A record on your site, then you would not be able to offer this flexibility.

The quirk of the DNS is that if you have a CNAME record for a host name, you cannot have any other records for that same host. However, your top level domain example.com usually must have an NS and SOA record. Therefore, you cannot also add a CNAME record for example.com.

The use of www.example.com gives you the opportunity to use a CNAME for www that points to your CDN, while leaving the required NS and SOA records on example.com. The example.com record will usually also have an A record to point to a host that will redirect to www.example.com using an HTTP redirect.

Solution 2:

Note: As of the ratification and implementation (by all current browsers, except possibly MSIE 11, see comments) of RFC 6265 in 2011 the following is no longer accurate, since cookies are by default never set across subdomains.

Historically, one good technical reason to make www.example.com canonical was that cookies of a main domain (i.e. example.com) were sent to all subdomains.

So if your site used cookies, they would be sent to all its subdomains.

Now, this often makes sense but it’s positively harmful if you only want to download static resources since it just wastes bandwidth. Consider all the style sheets and images on your website: usually, there’s no reason to send cookies to the server when requesting an image resource.

A good solution is therefore to use a subdomain for static resources, such as static.example.com, to save bandwidth by not sending cookies. All images and other static downloads can be downloaded from there. If you now use www.example.com for the dynamic content, this means that cookies only have to be sent to www.example.com, not to static.example.com.

If, however, example.com is your main site, then cookies will be sent to all subdomains, including static.example.com.

Now this isn’t relevant for most sites but changing your canonical URL later on isn’t a good idea so once you settled for example.com instead of www.*, you’re basically stuck with it.

An alternative is to use a whole different URL for static resources. Stack Overflow for example uses sstatic.net, YouTube uses ytimg.com etc. …

Solution 3:

www is a subdomain usually used for the web server on a domain along with others for other purposes such as mail etc. Nowadays, the subdomain paradigm is unnecessary; if you connect to a website in a browser, you'll get the website, or sending mail to the server will use its mail service.

Using www or not is a matter of personal preference. Opposing points of view can be found at http://no-www.org/ and http://www.yes-www.org/ - however, I believe that www is unnecessary and just adds more cruft to the URI.

Most servers send the same site either way, but don't redirect. For SEO purposes, choose one, then get the other to redirect to it. For example, some PHP code to do this:

if (preg_match('/www/', $_SERVER['SERVER_NAME'])) {
  header("Location: http://azabani.com{$_SERVER['REQUEST_URI']}");
  exit;
}

However, some reasons promoting the use of a www subdomain made by other answerers are great too, such as not sending cookies to static servers (credit Konrad Rudolph).