www vs apex: pick one host and redirect the other
https://example.com/ and https://www.example.com/ are different URLs to a crawler. If both return the page, every URL on the site exists twice, engines split ranking signals between the copies, answer engines may quote either, and analytics splits too. The fix is to choose one host, redirect the other to it permanently, and say so in every place a URL is written.
Neither choice is better for ranking. Apex (bare domain) is shorter; www gives some CDNs and DNS providers more flexibility (CNAME at the apex is not standard DNS, though Cloudflare, Route 53 and others flatten it). Pick the one your site already ranks for and keep it.
The rules
- 301 (or 308) from the non-canonical host to the canonical one, for every path, preserving path and query string. A 302/307 says "temporary" and engines keep both hosts.
- http:// to https:// on both hosts, also 301. Ideally one hop: http://www → https://apex directly, not via https://www.
- rel="canonical" on every page, absolute, on the canonical host. Tracking parameters and alternate hosts then collapse onto it.
- Sitemap, robots.txt Sitemap line, Open Graph og:url, IndexNow pings, email links and OAuth callbacks all use the canonical host. Never reference the other one.
- Both hosts must resolve. A www that returns NXDOMAIN or a certificate error loses the visitors who type it.
Where to set the redirect
- Name
nginx server block- Type
config- Value
server { listen 443 ssl; server_name www.example.com; return 301 https://example.com$request_uri; }
- Cloudflare: Rules → Redirect Rules (dynamic redirect with concat("https://example.com", http.request.uri.path) and status 301); SSL/TLS → Edge Certificates → Always Use HTTPS for the http → https part.
- Firebase Hosting: "redirects" in firebase.json, or the custom-domain redirect setting when both domains are connected.
- Vercel: "redirects" in vercel.json, or set the primary domain in the project and let Vercel redirect the alias.
- Netlify: [[redirects]] in netlify.toml with force = true, or the primary domain setting.
- nginx: a server block for the non-canonical host with return 301 https://example.com$request_uri;
- Apache: RewriteCond %{HTTP_HOST} ^www\. then RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L] in .htaccess.
How to check it
Request each combination (http/https × www/apex) without following redirects and read the status and Location header. AstraVerify’s Site plumbing card does this: it records the host your root page ends on as the canonical host, checks that the other host redirects there with a 301/308, that http redirects to https, and that the root page is reached in at most one hop. The Page metadata card checks the canonical tag points at the same host.
Frequently asked questions
- Will changing from www to apex hurt my ranking?
- A permanent redirect passes signals to the new host; expect a short period of fluctuation. If the site already ranks well on one host, keep that one rather than switching for taste.
- Do I still need canonical tags if the redirect is in place?
- Yes. The redirect handles the host; the canonical tag handles query strings, trailing slashes and any other duplicate URL of the same page.
- Why does my www return a certificate error?
- The certificate covers only the apex (or only www). Issue one that covers both, or terminate TLS at a CDN that does; the redirect must happen after a valid TLS handshake to be followed.
- What about subdomains like app.example.com?
- They are separate hosts by design. Keep marketing pages on the canonical host, application pages on the app subdomain, and do not let the app host serve the marketing pages.
Check your own domain. The scan shows your live records, a score out of 100 and the exact record to publish for each fix.
Related guides
- How the Discoverability score is calculated
- robots.txt for search and AI crawlers: what to allow
- JSON-LD structured data for small business sites
Canonical: https://astraverify.com/www-vs-apex-canonical-host