www or no www: pick one and redirect

www, non-www, canonical, htaccess, redirect, seo · 0 found this helpful

Your site can answer at both example.ca and www.example.ca. Search engines may treat these as two sites. Pick one, called your canonical address, and redirect the other to it.

Both work equally well. Most new sites choose no-www because it's shorter.

Before you start

  • Both example.ca and www.example.ca have DNS records. See DNS records.
  • Back up .htaccess.
  • Make sure WordPress uses the same choice in Settings → General.

Redirect www to no-www, and force HTTPS

# Send www and http to https://example.ca in one step
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.ca$ [NC]
RewriteRule ^(.*)$ https://example.ca/$1 [L,R=301]

Redirect no-www to www, and force HTTPS

# Send bare domain and http to https://www.example.ca
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.ca$ [NC]
RewriteRule ^(.*)$ https://www.example.ca/$1 [L,R=301]

Replace example.ca with your domain, and escape dots with \ as shown.

Test

curl -I http://www.example.ca
curl -I https://example.ca

The first should return 301 with a location: https://example.ca/. The second should return 200.

If something goes wrong

  • Too many redirects: WordPress or a plugin has the opposite preference. Make them agree.
  • Certificate warning on the www version: AutoSSL covers both names once DNS exists for both. See Free SSL with AutoSSL.
  • Rules ignored: they sit below another rule that ends processing with [L]. Move them to the top of the file.

Was this helpful?

Still stuck? Ask us and we'll walk you through it.