Skip to content

www vs non-www: choose one host and redirect the rest

www vs non-www redirect setup: pick one canonical origin, send the other three variants there with one permanent hop, and test all four with curl.

By Crawlito · 8 min read

In short

  • Every domain has four origins, http and https on www and non-www, and crawlers treat each one as a separate site with its own robots.txt.
  • Google's guidance is to pick one URL as canonical and redirect the other variants to it; it does not state a preference for www or non-www.
  • Google says the Change of Address tool is not needed for HTTP to HTTPS moves or for switching between www and non-www on the same domain.
  • Google recommends server-side permanent redirects such as 301 or 308, straight to the final destination, kept as long as possible and generally at least a year.
  • HSTS supports the setup: RFC 6797 says browsers ignore the header over plain HTTP, so it never replaces the server's HTTP to HTTPS redirect.
On this page

It does not matter much whether you choose www.example.com or example.com. What matters is that you choose one, and that the other three variants of your domain answer with a single permanent redirect to it. Google's own guidance is to "pick one of those URLs as your canonical URL, and use redirects to send traffic from the other URLs to your preferred URL", and its documentation does not state a preference between www and non-www. Problems usually come from leaving two hosts live, redirecting through a chain, or letting canonical tags and sitemaps name a different host than the redirects do.

This post covers the host-level setup: the four origins, the redirect target state, HSTS, and a curl test for all four. For page-level canonical conflicts, see canonical tags and duplicate URLs. For how robots.txt files are scoped, see robots.txt on subdomains, protocols and ports. Facts come from Google Search Central, RFC 6797, RFC 6265, hstspreload.org and sitemaps.org, read on 15 November 2026.

Why every domain has four origins

To a browser or a crawler, an origin is the scheme, the host and the port together. One registered domain usually gives you four of them:

OriginUsual role
http://example.comRedirect only
http://www.example.comRedirect only
https://example.comCanonical, or redirect only
https://www.example.comCanonical, or redirect only

Each one is a separate site in several ways that matter for crawling:

  • robots.txt. Google's robots.txt guide says the rules "apply only to the host, protocol, and port number" the file is served from, and its example states that https://www.example.com/robots.txt does not apply to https://example.com/.
  • Sitemaps. The sitemaps protocol says all URLs in a sitemap "must use the same protocol" and "reside on the same host as the Sitemap". Under that default rule, a sitemap on the www host cannot list non-www URLs.
  • Search Console. A URL-prefix property only includes URLs with that exact prefix, protocol included. Google's property help page says a Domain property includes all subdomains and protocols, which is why the Domain property is the easier way to see all four origins at once.
  • TLS certificates. Google's consolidation guide warns against implementing your certificate "for the wrong host-variant". If the certificate only covers www.example.com, a request to https://example.com fails before your redirect can run.

So "www vs non-www" is really a four-way decision, and three of the four origins should do nothing but redirect.

Which host to pick

Pick on practical grounds, then never change it without a reason:

  • Keep what you already have. If one host has years of links, citations in AI answers and bookmarks pointing at it, make that one canonical. Switching later is possible but needs redirects you then have to keep.
  • Look at your platform. Some hosting platforms and DNS setups make one form simpler. Follow what your host documents rather than fighting it.
  • Do not pick for cookie reasons alone. Under RFC 6265, a cookie set without a Domain attribute goes back only to the host that set it, and a cookie with Domain=example.com reaches every subdomain whichever host set it. If you run app. or shop. subdomains, check the Domain attributes your cookies use rather than the host name.

Whichever you pick, HTTPS should be the scheme. Google's consolidation guide says "Google prefers HTTPS pages over equivalent HTTP pages as canonical", and it lists HTTPS-to-HTTP redirects and bad certificates as things that work against that preference.

The target state: one permanent hop

Once the canonical origin is chosen, the goal is simple to state:

RequestResponse
http://example.com/pricing301 to https://www.example.com/pricing
http://www.example.com/pricing301 to https://www.example.com/pricing
https://example.com/pricing301 to https://www.example.com/pricing
https://www.example.com/pricing200

Three details make this work:

  1. Permanent and server-side. Google's site move guide recommends "HTTP permanent redirects if possible, such as 301 and 308", and server-side redirects where technically possible. The differences between the codes are covered in 301, 302, 307 and 308 redirects.
  2. Path and query preserved. Each variant should redirect to the same path on the canonical host, not to the homepage. A host redirect that drops the path turns every old link into a homepage visit.
  3. Straight to the final URL. The same guide advises "redirecting to the final destination directly". The common chain is http://example.com to https://example.com to https://www.example.com, often with a trailing slash redirect added on the end. Where that is not possible, Google says to keep the chain short, "ideally no more than 3 and fewer than 5", but one hop is the cleaner target.

Here is one way to do it in nginx, with www as the canonical host:

nginx
# Both plain-HTTP hosts: one hop straight to the canonical origin
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}

# HTTPS on the apex: certificate must cover example.com too
server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate     /etc/ssl/example.com/fullchain.pem;
    ssl_certificate_key /etc/ssl/example.com/privkey.pem;
    return 301 https://www.example.com$request_uri;
}

# The canonical origin
server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate     /etc/ssl/example.com/fullchain.pem;
    ssl_certificate_key /etc/ssl/example.com/privkey.pem;
    add_header Strict-Transport-Security "max-age=31536000" always;
    # ... site configuration
}

If a CDN or edge sits in front of your server, put the host redirect in one place only. Two layers that both redirect, each with slightly different rules, are the usual source of loops and two-hop chains.

Google describes a redirect as "a strong signal that the target of the redirect should become canonical". The other signals should name the same origin, so that nothing contradicts it:

  • Canonical tags on every page of the canonical origin use the full canonical URL, https://www.example.com/..., not a relative path and not the other host.
  • The sitemap lives on the canonical origin and lists only canonical-origin URLs, which the sitemaps protocol requires anyway. Google calls sitemap inclusion a weak signal, and "all pages listed in a sitemap are suggested as canonicals", so a sitemap full of non-www URLs argues against your redirects.
  • The robots.txt Sitemap: line gives the absolute URL of that sitemap on the canonical host.
  • Internal links, hreflang, Open Graph URLs and structured data use the canonical origin. Google's guide says to "link to the canonical URL rather than a duplicate URL".

The robots.txt question for the three redirecting origins is mostly settled by the redirect itself: if /robots.txt on each of them redirects to the canonical file, that file governs. The rules for that case are in the robots.txt hosts and protocols guide. What you want to avoid is a redirecting host that serves a different, stale robots.txt from an old platform.

Where HSTS fits

HTTP Strict Transport Security tells browsers to use HTTPS for a host they have already visited securely. RFC 6797 defines the header with a required max-age in seconds and an optional includeSubDomains:

http
HTTP/2 200
strict-transport-security: max-age=31536000; includeSubDomains

It supports the setup but does not replace any of it:

  • It works only after a secure visit. Section 8.1 says a browser "MUST ignore" the header when it arrives over insecure transport, and section 7.2 says a host "MUST NOT include" it in plain-HTTP responses. Your port 80 redirect still does the work for first visits and for any client that does not keep HSTS state.
  • It is a browser rule, not a crawler rule. No AI crawler operator documents keeping HSTS state at the time of writing, so treat the server redirect as what crawlers see.
  • Google lists it as support for HTTPS. "Implement HSTS" appears in Google's consolidation guide as one way to reinforce the HTTPS preference, though the same guide says HSTS cannot override the effect of a bad certificate or an HTTPS-to-HTTP redirect.
  • includeSubDomains is a commitment. It covers every subdomain of the host that sets it. Check that every subdomain serves HTTPS before you add it. Setting max-age=0 tells browsers to drop the policy.

If you plan to submit the domain to the HSTS preload list, read its requirements first. At the time of writing, hstspreload.org asks you to redirect from HTTP to HTTPS on the same host if you listen on port 80, and to serve the header on the base domain with a max-age of at least 31536000 seconds, includeSubDomains and preload. It also says an additional redirect from your HTTPS site must still carry the HSTS header, so the apex redirect block in the nginx example above would need its own add_header. For a www-canonical site, the same-host rule means http://example.com goes first to https://example.com and then to www: a two-hop path for that one variant, which is within Google's guidance on short chains.

Test all four variants with curl

Four requests, one per origin, show the whole picture. Use a real path, not only the homepage, because path handling is where host redirects most often break:

bash
for u in http://example.com http://www.example.com https://example.com https://www.example.com; do
  curl -s -o /dev/null -w "%{url_effective}  %{http_code} -> %{redirect_url}\n" "$u/pricing?ref=test"
done

You want three lines ending in 301 -> https://www.example.com/pricing?ref=test and one line ending in 200 -> with nothing after it. Then count the hops for each variant:

bash
for u in http://example.com http://www.example.com https://example.com; do
  curl -s -L -o /dev/null -w "$u  hops=%{num_redirects}  final=%{url_effective}\n" "$u/pricing"
done

Read the results this way:

ResultMeaningFix
200 on two originsTwo live copies of the siteRedirect the non-canonical one
302 or 307Temporary redirectChange it to 301 or 308
hops=2 or moreA chainPoint each variant straight at the canonical URL
Redirect lands on /Path droppedPass the request path and query through
000, curl exit code 60Certificate rejected, often because it does not cover that hostAdd the host to the certificate

Repeat the first loop for /robots.txt and /sitemap.xml. Then fetch the canonical page and confirm its canonical tag names the same origin. The curl guide for viewing pages as a crawler shows how to send a crawler user agent with the same requests.

Frequently asked questions

Is www or non-www better for SEO?

Google's documentation does not state a preference between them. What it asks for is one canonical URL, with redirects from the other variants.

Do I need the Change of Address tool to switch from www to non-www?

No. Google's site move guide says the tool is not needed for switching between www and non-www on the same domain, or for HTTP to HTTPS moves. You do need permanent redirects from every old URL to its new one.

How long should I keep the host redirects?

Google recommends keeping redirects for as long as possible, generally at least a year, and suggests considering keeping them indefinitely. For host redirects there is rarely a reason to remove them.

Does HSTS replace the HTTP to HTTPS redirect?

No. RFC 6797 says browsers ignore the header over plain HTTP, so it only applies after a secure visit, and crawlers are not documented to use it. Keep the server-side 301.

Do AI crawlers follow host redirects?

Major AI crawler operators do not document their redirect handling in detail at the time of writing. A single permanent hop to a URL that returns 200 is the setup least likely to cause problems for any client.

Check your canonical host

Once the four variants resolve in one hop, run the free check on an important page of your canonical origin to confirm crawlers can fetch it and that its canonical tag agrees. If you run several domains or subdomains and want every host variant tested with evidence, an audit covers them together.

Filed under Access and robots.txt. Topics: redirects, canonical, robots-txt, curl