Skip to content

How to redirect www to non-www (or the other way)

Best practices1 min readFixes check www-redirect

Serving the site on two hostnames splits caches, cookies and ranking signals. Pick one and redirect the other with a 301.

Step by step, with screenshots: ERR_TOO_MANY_REDIRECTS: how to fix a redirect loop →

Check your own site

Runs this check and the other 192, free, in about 45 seconds.

Or run only this check with the free Redirect checker.

What it is

A www redirect is the server rule that sends every request for one form of your domain, such as www.example.com, to the other, example.com, with a permanent 301 and the same path. It is what makes the two hostnames behave as one site.

When both answer, visitors and tools see two separate sites: cookies set on one are missing on the other, so logins and carts break when people switch; analytics splits sessions; and each hostname has its own cache. The redirect itself should be quick: one hop, straight to https on the chosen host, with the path and query string kept.

OK looks like this: both hostnames have DNS records and valid certificates, and the one you do not use returns a single 301 to the same URL on the one you do. getReport requests the other variant and warns when both serve the site.

What a passing site looks like

  • www and non-www redirect: passeswarning · −1 ptseffort M

www and non-www redirect

Why it matters. When www and non-www both answer, visitors and tools see two sites with separate cookies, caches and analytics sessions. One should redirect to the other.

How to fix it.
  1. Pick one host and 301-redirect the other to it, keeping the path; most hosts and CDNs have a one-click setting for this.
  2. Use the chosen host everywhere (links, sitemap, analytics property, social profiles).

Questions people ask

How do you redirect non-www to www?

Create a permanent 301 redirect from example.com to www.example.com that keeps the path and query string, in one hop to https. On Apache use a RewriteCond on the host and a RewriteRule; on nginx a separate server block with return 301 https://www.example.com$request_uri. Most hosts and CDNs have a setting for it.

Should the www redirect be a 301 or a 302?

A 301 (or 308), because the choice of hostname is permanent. A permanent redirect lets browsers cache it and tells search engines to consolidate everything on the chosen host. A 302 says the move is temporary, so the other hostname can linger in search results.

Do you need DNS records for both www and non-www?

Yes. The hostname you redirect from must still resolve and have a valid certificate, otherwise visitors who type it get an error instead of the redirect. Usually the bare domain has an A or AAAA record, www a CNAME to it or to your host, and the certificate covers both names.

How do you redirect www to non-www in WordPress?

Set both the WordPress Address and Site Address under Settings → General to the non-www URL; WordPress then redirects www requests to it. For a faster single hop, add the redirect at the server or CDN level instead, and make sure it goes straight to https.

How do you redirect www to non-www in Cloudflare?

Keep a proxied DNS record for www, then create a redirect rule for requests to www.example.com that sends a 301 to https://example.com with the path and query string preserved. Cloudflare offers a ready-made template for this under Rules → Redirect Rules.

Filed under Best practices. Copy is generated from the same catalogue that scores every report, so what you read here is what the report says.