How to Fix www vs non-www Duplicate Hostnames with 301s
If both example.com and www.example.com return your pages with a 200 status, search engines see two copies of every URL. By the end of this guide you will have chosen one canonical hostname, and the other will permanently redirect (301 or 308) to it with the path preserved.

Why it matters
When two hostnames serve the same content, backlinks, internal links and shared URLs split between them. Google has to guess which version to index, and crawl budget is spent on duplicates. AI answer engines and link previews may also cite the version you don't want.
A single permanent redirect consolidates ranking signals onto one hostname and gives users and crawlers one consistent address. A temporary redirect (302 or 307) still leaves search engines unsure which hostname is permanent.
What you need
Step-by-step fix
Choose your canonical hostname
Pick either example.com or www.example.com and stick with it. Use the version that already carries most of your history:
- the hostname in your existing
<link rel="canonical">tags and XML sitemap - the one Google shows in
site:example.comresults - the one most of your backlinks point to
Neither option ranks better on its own. Consistency is what matters.
Test what the other hostname returns today
Request the non-preferred hostname over both http and https, and note the status code and the Location header. A 200 means you have duplicate hostnames. A 302 or 307 works for users but is temporary.
A 301 or 308 pointing at your chosen host is the goal. If the hostname doesn't resolve at all, there is no duplicate, but visitors who type it will get an error.
curl -sI http://www.example.com/pricing | grep -iE '^(HTTP|location)' curl -sI https://www.example.com/pricing | grep -iE '^(HTTP|location)'
Make sure DNS and SSL cover both hostnames
The redirecting hostname must still resolve, and it must have a valid certificate. com shows a certificate error before the redirect can happen. g.
`) for the alternate host. Then issue a certificate that lists both names.
sudo certbot --nginx -d example.com -d www.example.com
Add a 301 redirect on nginx
Send all HTTP traffic for both names, plus HTTPS traffic for the non-preferred name, to the canonical HTTPS host in a single hop. Keep $request_uri so /pricing goes to /pricing, not to the homepage. com` on port 443 continues to serve the site.
Run sudo nginx -t && sudo systemctl reload nginx afterwards.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}Or add the redirect in Apache .htaccess
On Apache or shared hosting (cPanel, Plesk), add these rules at the top of the .htaccess file in your web root, above any CMS rules. The flag R=301 makes the redirect permanent. Without it, Apache defaults to a 302.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]Align canonicals, sitemap and CMS settings
Every internal signal should use the chosen hostname. If it doesn't, you create redirect hops and mixed signals. Update these:
<link rel="canonical">tags- XML sitemap URLs and the
Sitemap:line in robots.txt - hard-coded internal links
- the site URL setting in your CMS
In Google Search Console, use a Domain property so both hostnames are covered, and resubmit the sitemap.
<link rel="canonical" href="https://example.com/pricing">
Verify the redirect from outside your network
Request the alternate hostname again over both protocols and on a deep URL, not just the homepage. You want a single 301 (or 308) whose Location starts with https://example.com and keeps the original path and query string. Clear any CDN cache first so you aren't seeing an old response.
curl -sI "https://www.example.com/pricing?ref=test" | grep -iE '^(HTTP|location)' # Expected: # HTTP/2 301 # location: https://example.com/pricing?ref=test
Platform-specific notes
WordPress
Under Settings > General, set both WordPress Address (URL) and Site Address (URL) to your chosen hostname. WordPress then issues a 301 for the other host, but only if requests for that host actually reach WordPress. Make sure the alternate hostname is attached to the same site in your host's panel. Managed hosts like Kinsta and WP Engine have a "primary domain" toggle that handles the redirect at the server level. If Settings > General is greyed out, the values are set by WP_HOME and WP_SITEURL in wp-config.php.
Shopify
You can't edit server config on Shopify. Go to Settings > Domains, click the hostname you want, and choose "Set as primary". Shopify automatically 301-redirects the other connected domains to the primary. Make sure both example.com and www.example.com are connected, not just one, or the missing one won't resolve.
Cloudflare
The www DNS record must be proxied (orange cloud) for rules to apply. Go to Rules > Redirect Rules > Create rule (or use the "Redirect from WWW to root" template). Set the match to Hostname equals www.example.com. Use a dynamic target of concat("https://example.com", http.request.uri.path), choose status 301, and enable "Preserve query string".
Next.js / Vercel
On Vercel, go to Project > Settings > Domains and add both hostnames. Edit the non-preferred one and set it to redirect to the other, choosing 301 or 308. Self-hosted, add a redirects() entry in next.config.js with has: [{ type: 'host', value: 'www.example.com' }], source '/:path*', destination 'https://example.com/:path*' and permanent: true. That setting returns a 308, which also passes.
Wix / Squarespace / Webflow
These platforms handle the redirect themselves once the domain is connected. In Wix, go to Settings > Domains. In Squarespace, go to Settings > Domains and set the primary domain. In Webflow, go to Site settings > Publishing and click "Make default" next to the chosen domain. Make sure the DNS records for both hostnames point to the platform.
Check that it worked
✅ How to verify
- Run curl -sI on http:// and https:// versions of the non-preferred hostname. Each should return a single 301 or 308 whose Location points to the canonical host.
- Load a deep URL with a query string on the alternate host in a browser. The address bar should switch to the canonical host with the same path and parameters.
- In Google Search Console, run URL Inspection on a www (or non-www) URL. Over the following weeks, Google should report the user-declared and Google-selected canonical on your chosen host.
- Re-run the site audit and confirm the www / non-www consistency check passes rather than warning or failing.
⚠️ Common mistakes
- Using a 302 or 307. It works for visitors, but it tells search engines the move is temporary, so the duplicate hostname can stay indexed.
- Redirecting every URL on the alternate host to the homepage instead of the matching path. This loses page-level rankings and creates soft 404s.
- Forgetting the HTTPS server block or the certificate for the alternate hostname. https://www.example.com then shows a security error instead of redirecting.
- Chaining redirects (http://www → https://www → https://example.com) instead of going straight to the final URL in one hop.
Is this still an issue on your site? Run a free audit — ZORO checks this and 294 other SEO, AI-visibility, accessibility and security signals, with fixes for your platform.
Check my site freeFAQ
Is www or non-www better for SEO?
Neither ranks better. Google treats them as separate hostnames, so what matters is choosing one and permanently redirecting the other to it. Keep whichever version your existing links, canonicals and indexed pages already use.
Should I use a 301 or 308 redirect for www to non-www?
Both are permanent and both consolidate signals, and Google treats them the same way. A 301 is the most widely supported choice. A 308 additionally preserves the request method, which is what Next.js and Vercel send by default.
Do I need a DNS record for www if I don't use it?
Not strictly. If the alternate hostname doesn't resolve, there is no duplicate content. However, visitors who type www.example.com will get a browser error. Keeping the record and redirecting it is the better experience.
Will switching from www to non-www hurt my rankings?
Expect some brief fluctuation while Google recrawls. With path-preserving 301 redirects, updated canonicals and an updated sitemap, signals transfer and rankings normally settle within a few weeks. Don't remove the redirect later; keep it in place permanently.
How to Fix a Missing robots.txt File (Step-by-Step)
How to Fix a Missing HSTS Header (Strict-Transport-Security)
How to Fix robots.txt Blocking CSS, JS and Key Pages
How to Fix SSL Certificate Expiry and Trust Errors
How to Fix a Missing XML Sitemap (and Get It Found)
How to Fix Mixed Content Warnings on an HTTPS Website