Choose one host (www.example.com or example.com) and one path style (with or without a trailing slash). Redirect every other form to it with a permanent redirect, and make your canonical tags, internal links and sitemap use only that form. Google's guidance is to pick one preferred canonical URL and use redirects to send traffic from the other forms to it [2]. Which form you pick matters less than doing it consistently.
What counts as a different URL
Each of these can be served as a separate URL, with its own copy of the same page:
http://example.com/guidehttps://example.com/guidehttps://www.example.com/guidehttps://example.com/guide/https://www.example.com/guide/
A URL path is a sequence of segments separated by slashes [7], so /guide and /guide/ are not guaranteed to be the same resource. Servers often make them the same, but you have to check. One exception: https://example.com and https://example.com/ are the same request, because the root path is always /. Don't spend time on that one.
Step 1: Pick the form
Decide the host and the slash rule in one sitting, using these defaults.
| Decision | Choose this if | Watch out for |
|---|---|---|
| www | Your DNS or CDN setup prefers a CNAME on a subdomain | Apex CNAME records are not allowed in standard DNS, so the bare domain needs an ALIAS or flattening feature, or a redirect service |
| non-www | You want shorter URLs and your host handles the apex cleanly | Cookies set on the apex are sent to all subdomains, which can matter if you run other subdomains |
| Trailing slash | Your site is built from directories or your framework outputs /page/index.html | Links to files with extensions (/logo.png/) must not get a slash |
| No trailing slash | Your routes are app routes, not directories | Same: stay consistent for every route, including the ones you add later |
If one form is already indexed and has most of your inbound links, keep it. Changing to the other form means redirecting your whole site, and that change has some risk. If you are starting fresh, take whatever your host and framework do by default so there is less configuration to maintain.
Step 2: Redirect everything else with a 301
MDN says the server should respond with an HTTP 301 redirect from the non-canonical domain to the canonical domain [2]. The same logic applies to the slash.
Redirect to the final URL in one hop, including the scheme. A request for http://www.example.com/guide/ should go straight to https://example.com/guide, not through three redirects. The chain problem is covered in redirect chains and loops.
nginx (target: https://example.com, no trailing slash)
# Everything on port 80, either host
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
# www over HTTPS
server {
listen 443 ssl;
server_name www.example.com;
# ssl_certificate lines here
return 301 https://example.com$request_uri;
}
# canonical host
server {
listen 443 ssl;
server_name example.com;
# ssl_certificate lines here
# strip a trailing slash (not for the root)
rewrite ^/(.+)/$ /$1 permanent;
# ... rest of your config
}
Apache .htaccess (target: https://example.com)
RewriteEngine On
# www or http -> https non-www, in one hop
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
Frameworks and hosts
Most frameworks have a setting for the slash. Check the exact option in your version's docs before changing it.
- Next.js:
trailingSlash: trueinnext.config.jsmakes routes use a trailing slash. The default is no slash. In both cases the framework redirects the other form to the configured one. - Astro: the
trailingSlashoption accepts'always','never'or'ignore'. Avoid'ignore'if you want one form. Thebuild.formatsetting decides whether static output is/page/index.htmlor/page.html, and your host's slash behavior depends on it. - Static hosts and CDNs (Netlify, Vercel, Cloudflare and similar): the domain-level setting for www or apex redirect is usually in the dashboard, and slash behavior is set in the project config. Set the host redirect there rather than in app code, so it happens before your app runs.
Some frameworks send a 308 instead of a 301. Both are permanent redirects in HTTP, but MDN's example names 301 [2], so use 301 where you control it. Where you can't, confirm the permanent status in the curl check below.
Step 3: Make canonical tags and the sitemap agree
Google ranks canonicalization signals by strength. Redirects and rel="canonical" are strong signals, and sitemap inclusion is a weaker one [3]. Google also says not to specify different canonical URLs for the same page using different techniques, for example a sitemap pointing to one URL while rel="canonical" points to another [3].
So on every page:
- The canonical tag is an absolute URL in the chosen form:
<link rel="canonical" href="https://example.com/guide">. - Internal links use the same form, so visitors and crawlers don't hit a redirect on every click.
- The sitemap lists only that form. Google recommends including only preferred canonical URLs [4], and says to list the preferred version rather than the duplicate when redirects handle trailing-slash normalization [8].
- Structured data URLs (
url,@id,item) use it too.
Details on tag behavior are in canonical tags explained, and sitemap building is in the XML sitemap guide.
Step 4: Check it with curl
Test the matrix from the command line. This prints the status code and the redirect target for each form:
for u in \
http://example.com/guide \
http://example.com/guide/ \
http://www.example.com/guide \
http://www.example.com/guide/ \
https://www.example.com/guide \
https://www.example.com/guide/ \
https://example.com/guide \
https://example.com/guide/
do
curl -s -o /dev/null -w "%{http_code} %{redirect_url} <- $u\n" "$u"
done
For a no-slash, non-www target, you want this:
| Request | Expected |
|---|---|
http://example.com/guide | 301 to https://example.com/guide |
http://www.example.com/guide/ | 301 to https://example.com/guide |
https://www.example.com/guide | 301 to https://example.com/guide |
https://example.com/guide/ | 301 to https://example.com/guide |
https://example.com/guide | 200, empty redirect target |
Then check three more things:
# 1. Canonical tag on the final URL
curl -s https://example.com/guide | grep -i 'rel="canonical"'
# 2. Every sitemap URL returns 200 directly (anything else is a mismatch)
curl -s https://example.com/sitemap.xml \
| grep -o '<loc>[^<]*' | sed 's/<loc>//' | head -50 \
| while read -r u; do curl -s -o /dev/null -w "%{http_code} $u\n" "$u"; done
# 3. Full chain length for a worst-case URL
curl -sIL -o /dev/null -w "hops: %{num_redirects} final: %{url_effective}\n" \
http://www.example.com/guide/
You want one hop at most. Any sitemap URL that returns a 301 means the sitemap uses a non-preferred form.
Step 5: Confirm in Search Console
Google's FAQ says to verify both the www and non-www versions in Search Console when looking at indexing data [1]. A Domain property covers all variants. If you use URL-prefix properties, add each form.
The URL Inspection tool shows a Google-selected canonical, which is useful when your preferred canonical and Google's choice differ [5]. Inspect a few URLs in the old form after the redirects go live. If Google's choice differs from yours, look first for a signal that contradicts the others: an internal link, a canonical tag or a sitemap entry in the wrong form.
Common mistakes
- Redirecting the host but not the slash, or the reverse. Test all combinations, not just one.
- Canonical tag generated from the request URL. The tag then echoes whatever was requested, and the duplicates "canonicalize" to themselves. Build it from a fixed base URL setting.
- Sitemap generated by a different tool than the site. It often defaults to the other slash style.
- A blanket slash rule that catches files, such as
/feed.xml,/robots.txtor images. Exclude paths with file extensions. - A CDN redirect and an origin redirect that disagree. They can create a loop or a two-hop chain. Keep the rule in one layer.
- Changing the rule halfway. Fix new routes, old routes and internal links in one release.
The broader question of when to redirect, canonicalize or noindex is in canonical vs redirect vs noindex.
What to do next
- Write down the chosen form in one line, for example
https://example.com/path(non-www, no trailing slash, HTTPS). - Set the redirects at one layer (CDN, web server or framework) so every other form lands on it in one hop.
- Run the curl matrix above and fix anything that isn't a single 301 or a direct 200.
- Make the canonical tags, internal links, structured data and sitemap use the same form.
- Inspect a few old-form URLs in Search Console and compare Google's selected canonical to yours.