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 1088689 of Choosing between www and non-www URLs

  • Revision slug: Web/HTTP/Basics_of_HTTP/Choosing_between_www_and_non-www_URLs
  • Revision title: Introduction to www and non-www URLs
  • Revision id: 1088689
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment reviewWeb/HTTP/Basics_of_HTTP/Introduction_to_www_and_non-www_URLs Web/HTTP/Basics_of_HTTP/Choosing_between_www_and_non-www_URLs
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:// is called the domain. It is the name of the 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 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 can't have two. What is important is that you are coherent and consistent with which one is the official domain. This official domain 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 two techniques so that it is clear for your users, or search engines, 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 domains as your canonical one! There are two techniques below to allow the non-canonical domain to work still.

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.
  3. The client issues a request to the canonical domain: https://example.org/whatddup

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 is the canonical address of a page. This has no impact on the human reader of the page, but tells search engine crawlers where the page actually lives. That way, search engines don't index the same page several times, which could lead to considering it as duplicated content or some kind of spam, and might then remove or lower your page from the search engine result pages.

When adding such a tag, you serve 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.

Make your page work for both

With the preivous techniques, you can configure your server to answer 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 actually want to use for your canonical location, and then redirecting the other one to it.

Deciding the case

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

<div>{{HTTPSidebar}}</div>

<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 id="What_are_domain_names">What are domain names?</h2>

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

<p>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 <em>one domain name represents one single server</em>.</p>

<h2 id="So_do_I_have_to_choose_one_or_the_other_for_my_site_web">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 can't have two. What is important is that you are coherent and consistent with which one is the official domain. <strong>This official domain 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 two techniques so that it is clear for your users, or search engines, 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 domains as your canonical one! There are two techniques below to allow the non-canonical domain to work still.</p>

<h2 id="Techniques_to_link_a_non-canonical_domain_to_its_canonical_equivalent">Techniques for canonical URLs</h2>

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

<h3 id="Using_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>
 <li>The client issues a request to the canonical domain: <code>https://example.org/whatddup</code></li>
</ol>

<p>The<a href="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">how to configure an Apache server to redirect one domain to the other</a>.</p>

<h3 id="Using_&lt;link_relcanonical&gt;">Using <em><code>&lt;link rel="canonical"&gt;</code></em></h3>

<p>It is possible to add a special HTML {{HTMLElement("link")}} element to a page to indicate what is the canonical address of a page. This has no impact on the human reader of the page, but tells search engine crawlers where the page actually lives. That way, search engines don't index the same page several times, which could lead to considering it as duplicated content or some kind of spam, and might then remove or lower your page from the search engine result pages.</p>

<p>When adding such a tag, you serve 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>

<h2 id="Make_your_page_work_for_both">Make your page work for both</h2>

<p>With the preivous techniques, you can configure your server to answer 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 actually want to use for your canonical location, and then redirecting the other one to it.</p>

<h2 id="How_to_choose_which_one_is_the_canonical_issue">Deciding the case</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/">Stats on what people type in the URL bar</a> (2011)</li>
</ul>
Revert to this revision