Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1087497 of Choosing between www and non-www URLs

  • Revision slug: Web/HTTP/Basics_of_HTTP/Introduction_to_www_and_non-www_URLs
  • Revision title: Introduction to www and non-www URLs
  • Revision id: 1087497
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Refactorization in progress
Tags: 

Revision Content

{{HTTPSidebar}}

A recurring question among website owners is whether to choose non-www or www URLs. This page provides advice on what's best to do.

What are domain names?

In an HTTP URL, the first substring that follows the initial https:// or https:// until the first '/' is called the domain. It is the name of the server where the document resides.

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

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

  • Yes, you need to choose one and stick with it. The choice of which one to have as your canonical location is yours, but if you choose one, stick with it. It will make your website appear more consistent to your users and to search engines. Sticking with one includes always linking to the chosen domain (which shouldn't be hard if you're using relative URLs in your website) and always sharing links (by email/social networks, etc.) to the same domain.
  • No,  you still can't have two. What is important is that you are coherent and consistent, in which one is the official name. This official name is called the canonical name. All your absolute links should use it. But even so, you can still have the other domain working: HTTP allows 2 techniques so that it is clear for your users, or search engine, which domain is the canonical one, while allowing the non-canonical domain to still work and provide the expected pages.

So, choose one of your domain as your canonical one and use it and use one of the two techniques below to allow the non-canonical domain to still work.

Techniques to link a non-canonical domain to its canonical equivalent

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 non-www URLs as canonical type, redirect all www URLs to their equivalent URL without the www.

Example:

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

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

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

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

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

You can have both

You can have both and probably should since you can't predict which URL users will type of their browser bar. It is a matter of choosing which type you actually want to use for your canonical location, and then redirecting the other one to it.

 

How to choose which one is the best

This is such a subjective topic that it can be considered a bikeshedding issue. There are many pages discussing this; have a read and make your own mind up. WWW vs non-WWW for your Canonical Domain URL – Which is Best and Why? provides a good write up.

See also

Revision Source

<p>{{HTTPSidebar}}</p>

<p class="summary">A recurring question among website owners is whether to choose non-www or www URLs. This page provides advice on what's best to do.</p>

<h2>What are domain names?</h2>

<p>In an HTTP URL, the first substring that follows the initial <code>https://</code> or <code>https://</code> until the first <code>'/'</code> is called the domain. It is the name of the server where the document resides.</p>

<p>A server doesn't necessarily a physical machine: several servers can reside on the same physical machine. On the contrary, one server can be handled by several machine, cooperating to produce the answer or balancing the load of the requests between them. The key point being that semantically, <em>one domain name represents one single server</em>.</p>

<h2>So, do I have to choose one or the other for my site web?</h2>

<ul>
 <li><u>Yes</u>, you need to choose one and stick with it. The choice of which one to have as your canonical location is yours, but if you choose one, stick with it. It will make your website appear more consistent to your users and to search engines. Sticking with one includes always linking to the chosen domain (which shouldn't be hard if you're using relative URLs in your website) and always sharing links (by email/social networks, etc.) to the same domain.</li>
 <li><u>No</u>,&nbsp; you still can't have two. What is important is that you are coherent and consistent, in which one is the official name. <strong>This official name is called the <em>canonical</em> name. </strong>All your absolute links should use it. But even so, you can still have the other domain working: HTTP allows 2 techniques so that it is clear for your users, or search engine, which domain is the canonical one, while allowing the non-canonical domain to still work and provide the expected pages.</li>
</ul>

<p>So, choose one of your domain as your canonical one and use it and use one of the two techniques below to allow the non-canonical domain to still work.</p>

<h2>Techniques to link a non-canonical domain to its canonical equivalent</h2>

<p>There are different ways to choose which website is <em>canonical</em>.</p>

<h3 id="HTTP_301_redirects">Using HTTP 301 redirects</h3>

<p>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,&nbsp;if you've chosen non-www URLs as canonical type, redirect all www URLs to their equivalent URL without the www.</p>

<p>Example:</p>

<ol>
 <li>A server receives a request for <code>https://www.example.org/whaddup</code> (when the canonical domain is example.org)</li>
 <li>The server answers with a code{{HTTPStatus(301)}} with the header {{HTTPHeader("Location<code>")}}: https://example.org/whaddup</code>.</li>
</ol>

<p>The<a href="https://github.com/h5bp/html5-boilerplate" title="https://github.com/h5bp/html5-boilerplate"> HTML5 boilerplate project</a> has an example on <a href="https://github.com/h5bp/html5-boilerplate/blob/7a22a33d4041c479d0962499e853501073811887/.htaccess#L219-L258" title="https://github.com/h5bp/html5-boilerplate/blob/7a22a33d4041c479d0962499e853501073811887/.htaccess#L219-L258">how to configure an Apache server to redirect one domain to the other</a>.</p>

<h3 id="Using_link_with_the_attirbute_relcanonical">Using <a href="/en-US/docs/HTML/Element/link" title="/en-US/docs/HTML/Element/link">link</a> with the attirbute <a href="https://support.google.com/webmasters/bin/answer.py?hl=en&amp;answer=139394" title="https://support.google.com/webmasters/bin/answer.py?hl=en&amp;answer=139394">rel="canonical"</a></h3>

<p>In such a case, you are serving the same content for both domains and telling search engines which URL is canonical. In the previous example, <code>https://www.example.org/whaddup</code> would serve the same content as <code>https://example.org/whaddup</code>, but with an additional {{htmlelement("link")}} element in the head:</p>

<p><code>&lt;link href="https://example.org/whaddup" rel="canonical"&gt;</code></p>

<p>Unlike in the previous case, the browser history will consider non-www and www URLs as separate entries.</p>

<p>You can have both</p>

<p>You can have both and probably should since you can't predict which URL users will type of their browser bar. It is a matter of choosing which type you actually want to use for your canonical location, and then redirecting the other one to it.</p>

<h3 id="Choose_one_and_stick_with_it!">&nbsp;</h3>

<h2 id="How_to_choose_which_one_is_the_best">How to choose which one is the best</h2>

<p class="entry-title">This is such a subjective topic that it can be considered a <a href="https://bikeshed.com/">bikeshedding</a> issue. There are many pages discussing this; have a read and make your own mind up. <a href="https://www.hyperarts.com/blog/www-vs-non-www-for-your-canonical-domain-url-which-is-best-and-why/">WWW vs non-WWW for your Canonical Domain URL – Which is Best and Why?</a> provides a good write up.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://www.chrisfinke.com/2011/07/25/what-do-people-type-in-the-address-bar/" title="https://www.chrisfinke.com/2011/07/25/what-do-people-type-in-the-address-bar/">Stats on what people type in the URL bar</a> (2011)</li>
</ul>
Revert to this revision