Home / Fix guides / Technical SEO

How to redirect non-www to www to avoid duplicate content

If your site loads at both example.com and www.example.com, pick one version and set up a permanent (301) redirect from the other to it. That gives search engines, visitors and your analytics a single address for every page.

Many websites answer at two addresses: https://example.com and https://www.example.com. If both load and neither sends visitors to the other, search engines can treat them as two separate copies of your whole site. The fix is simple in principle: choose one version as the main one, then make the other redirect to it with a permanent 301 redirect.

This page uses www as the preferred version, since it is a common choice. Redirecting www to non-www works the same way in reverse, and either choice is fine. What matters is that you choose one and stick to it.

What the problem looks like

You type your domain with and without www and the page loads both times. The address bar keeps whatever you typed. Nothing jumps from one to the other.

You may also notice symptoms elsewhere:

  • Search results showing some pages with www and others without.
  • Analytics reports listing the same page under two addresses.
  • Visitors logged in on one version but logged out on the other, because cookies belong to a specific hostname.
  • A browser security warning on one version, if your SSL certificate does not cover both names.

Why it matters

Search engines can usually work out that the two versions are the same site, but you are leaving that decision to them. In the worst case, links and signals from other sites get split between two addresses, the wrong version shows in results, and crawlers spend time on pages that add nothing.

There are practical side effects too. Split analytics make it hard to see what is really working. A certificate warning on one version looks alarming to a customer who typed your address the short way. A single clean address is simply easier to trust and to measure.

How to check if your site has it

These checks are free and take a few minutes.

  1. Open a private browser window and visit http://example.com, https://example.com, http://www.example.com and https://www.example.com (use your own domain). All four should end up at the same final address.
  2. If you are comfortable with a terminal, run curl -I http://example.com and look at the first lines. A correct setup shows 301 and a location: line pointing to your preferred version. A 200 means that version is answering by itself.
  3. In Google Search Console, check whether both versions appear in the Pages report or in the URLs listed under performance. If you only verified one version, you may not see the other.
  4. Search Google for site:example.com and scan the results for a mix of www and non-www addresses.

The week 1 SEO review from SiteMonday looks for this, along with other basic search problems, and lists it in a ranked fix list if both versions are live.

How to fix it

The fix has four parts, whatever platform you use.

  1. Choose your preferred version. If your site is already indexed, pick the one that most of your current search results use, to avoid unnecessary change.
  2. Make sure both names reach your site. In your DNS settings, the www name is usually a CNAME record or an A record, and the bare domain is usually an A record. Both must point at your host, otherwise the redirect has nothing to answer it.
  3. Cover both names with SSL. The redirect for https visitors only works without warnings if your certificate includes both example.com and www.example.com. Most hosts and Let's Encrypt certificates handle this, but check.
  4. Add a permanent 301 redirect from the other version to your preferred one, keeping the page path. example.com/shop/ should go to www.example.com/shop/, not to the home page.

Use 301 (permanent), not 302 (temporary). A 301 tells search engines the move is lasting.

Fixing it on common platforms

Where the setting lives depends on your host, theme and version, so treat these as pointers to where to look.

  • WordPress: go to Settings, then General, and set both the WordPress Address and Site Address to the www version. WordPress then usually redirects the other version itself. Some hosts override this, so test afterwards. See the WordPress guide.
  • Cloudflare: look for Rules, then Redirect Rules (older accounts may show Page Rules). Create a rule that sends the bare domain to the www version with a 301 and keeps the path. The DNS record for the bare domain must be proxied (orange cloud) for this to apply. See the Cloudflare guide.
  • cPanel, GoDaddy, Namecheap: these usually have a Redirects or Domain Forwarding section. Choose a permanent 301 redirect, with or without www as your source, and the full www address as the target. Each has its own guide in the platform list for this problem.
  • Website builders (Wix, Squarespace, Shopify): these normally have a primary domain setting under Domains. Set the www version as primary and the builder handles the redirect.

If you manage your own server, these are typical examples. Replace example.com with your domain.

Apache, in the .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Nginx, as its own server block (with your certificate lines inside):

server {
    listen 443 ssl;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
}

Back up the file before editing, and ask your developer or host if you are unsure. A mistake here can create a redirect loop that takes the site down.

How to confirm the fix worked

Repeat the checks you did earlier. All four address variants should end at your single preferred version, and curl -I should show a 301 with the correct location: value. Try a deep page, not just the home page, to confirm the path is kept.

Also watch for a redirect loop (the browser says the page redirected too many times) and for chains where one redirect leads to another before reaching the end. Ideally http and non-www go to the final https www address in one step.

Once the redirect is working, in Google Search Console add the preferred version as a property if you have not already, and check the Pages report over the following weeks. Old addresses can take a while to drop out of results, which is normal.

How to stop it coming back

  • Set canonical tags on your pages that point to the preferred version. This is a useful second layer of protection, and the canonical tags guide explains how.
  • Use the preferred version in your sitemap, internal links, social profiles and email signatures.
  • Re-test after changing hosts, moving to a new CDN, renewing certificates or switching themes, since these are the moments redirects quietly disappear.
  • Check again after any site rebuild before it goes live.

A quick four-address check every few months costs nothing and catches this early.

Step by step for your platform

Questions

Should I use www or non-www?

Either works. Choose the one your site already uses most in search results, then redirect the other to it and use it everywhere.

Is a 301 or 302 redirect better for this?

Use a 301, which is permanent. A 302 says the move is temporary, which is not what you mean here.

Will Google penalize me for having both versions?

There is usually no direct penalty, but links and signals can be split and the wrong version can show in results. Redirecting removes that uncertainty.

Do I need to redirect http to https as well?

Yes, ideally all four variants should end at one https address. Aim for as few redirect steps as possible.

How long until the change shows in Google?

It varies, and old addresses can take days or weeks to drop out of results. Keep the redirect in place permanently.

Will redirecting hurt my rankings?

A correct 301 that keeps each page's path normally passes its value to the new address. Rankings can shift briefly while search engines reprocess your pages.

Related problems