Home » Website Errors » WWW vs Non-WWW: Pick One Version and Redirect the Other
WWW vs Non-WWW: Pick One Version and Redirect the Other
Short answer: www.example.com and example.com are technically different addresses. If both load your site, search engines can treat them as two separate sites with the same content. Choose one as your main version and send the other to it with a permanent (301) redirect.
Step 1: check what happens today
Run these from any computer with curl (built into Windows 10 and later, macOS and Linux):
curl -sI http://example.com
curl -sI http://www.example.com
curl -sI https://example.com
curl -sI https://www.example.com
For each, read the first line and the location: header. The ideal result is that three of the four answer 301 with a location: pointing to your chosen version, and only one answers 200. If two or more answer 200, you have a duplicate version problem. If you see a long chain of redirects, see Too Many Redirects.
Step 2: choose your main version
Either works. Pick based on practical points:
| Choice | Good for | Watch out for |
|---|---|---|
example.com (non-www) |
Shorter, cleaner links | Some DNS providers cannot put a CNAME on the root domain |
www.example.com |
Easier to move behind a CDN or load balancer with a CNAME | Slightly longer |
If your site is already established, keep the version that most of your links and search results use. Changing it later means more redirects to manage.
Step 3: make sure both names point at your site
Both names need DNS records, otherwise the one you redirect away from will not even connect. Typically that is an A record for the root and either an A or CNAME record for www. Check with dig A example.com +short and dig A www.example.com +short. Both should return the address of your host. If a freshly registered domain is not connecting at all, see New Domain Not Working.
Step 4: add the 301 redirect
On Apache, add this to .htaccess to send non-www to www (swap the names for the opposite direction):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
On Nginx:
server {
listen 80;
listen 443 ssl;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
WordPress sites also have a setting for this: the Site Address in Settings, General. Make sure it matches your chosen version. Many hosts and CDNs also offer a one-click redirect rule, which is simpler and safer than editing files by hand. Back up the file before changing it.
Step 5: make everything agree
A redirect alone is not enough. Update:
- canonical tags so they name the chosen version
- your XML sitemap, so it lists only the chosen version
- internal links and any hard-coded URLs
- the property you verify in Google Search Console
Then rerun the four curl commands from Step 1 to confirm.
Common mistakes
- Redirecting with a 302 instead of a 301.
- Redirecting only the homepage and not every path (the redirect should keep the page path, as
$request_uridoes above). - Forgetting the HTTPS certificate for the version being redirected.
- Leaving old links, sitemap entries or canonical tags pointing at the other version.
- Creating a loop because the host and the CMS each redirect in opposite directions.
Frequently Asked Questions
Is www or non-www better for SEO?
Neither ranks better. What matters is choosing one, redirecting the other to it, and using that version in links, your sitemap and canonical tags.
Should I use a 301 or a 302 redirect?
Use a 301 (permanent) redirect for a lasting choice. A 302 tells search engines the move is temporary, so they may keep treating the old address as the main one.
Do I need an SSL certificate for both versions?
Yes. Visitors who type the version you redirect away from still connect over HTTPS first, so the certificate must cover both www.example.com and example.com or they will see a warning before the redirect.
Found a mistake or still stuck? Contact us. See how we write and check these guides in our editorial policy, or read about DocumentGuy.