docs.rodeo

MDN Web Docs mirror

Choosing between www and non-www URLs

A recurring question among website owners is whether to choose ‘www’ (www.example.com) or ‘non-www’ (example.com) URLs. This page provides some advice on what’s best.

What are domain names?

In an HTTP URL, the first substring that follows the initial http:// or https:// is called the domain name. This domain name is hosted on a server where the document resides.

A server isn’t necessarily a physical machine: several servers can reside on the same physical machine. Or, one server can be handled by several machines, cooperating to produce the answer or balancing the load of the requests between them. The key point is that semantically one domain name represents one single server.

So, do I have to choose one or the other for my website?

So, choose one of your domains as your canonical one! There are two techniques below to allow the non-canonical domain to work still.

Techniques for canonical URLs

There are different ways to choose which website is canonical.

Using HTTP 301 redirects

In this case, you need to configure the server receiving the HTTP requests (which is most likely the same for ‘www’ and ‘non-www’ URLs) to respond with an adequate HTTP {{HTTPStatus(301)}}  response to any request to the non-canonical domain. This will redirect the browser trying to access the non-canonical URLs to their canonical equivalent. For example, if you’ve chosen to use ‘non-www’ URLs as the canonical type, you should redirect all ‘www’ URLs to their equivalent URL without the ‘www’.

Example:

  1. A server receives a request for http://www.example.org/whaddup (when the canonical domain is example.org).
  2. The server answers with a code {{HTTPStatus(301)}}  with the {{HTTPHeader("Location")}}  header Location: http://example.org/whaddup.
  3. The client issues a request to the location under the canonical domain: http://example.org/whaddup.

The HTML5 boilerplate project has an example on how to configure an Apache server to redirect one domain to the other.

It is possible to add a special HTML {{HTMLElement("link")}}  element to a page to indicate what the canonical address of a page is. This has no impact on the human reader of the page, but tells search engine crawlers where the page actually lives. This way, search engines don’t index the same page several times, potentially leading to it being considered as duplicate content or spam, and even removing or lowering your page from the search engine result pages.

When adding such a tag, you serve the same content for both domains, telling search engines which URL is canonical. In the previous example, http://www.example.org/whaddup would serve the same content as http://example.org/whaddup, but with an additional {{htmlelement("link")}}  element in the head:

<link href="http://example.org/whaddup" rel="canonical" />

Unlike the previous case, browser history will consider non-www and www URLs as independent entries.

Make your page work for both

With these techniques, you can configure your server to respond correctly for both, the www-prefixed and the non-www-prefixed domains. It is good advice to do this since you can’t predict which URL users will type in their browser’s URL bar. It is a matter of choosing which type you want to use as your canonical location, then redirecting the other type to it.

Deciding the case

This is a very subjective topic — it could be considered a bikeshedding issue. If you wish to read deeper, here are some resources:

See also

In this article

View on MDN