I need my site to consistently use www and HTTPS, redirecting the non-www and HTTP versions, and I want the htaccess done correctly rather than cobbled from conflicting examples.
What is the correct htaccess for canonical www and HTTPS redirects?
I need my site to consistently use www and HTTPS, redirecting the non-www and HTTP versions, and I want the htaccess done correctly rather than cobbled from conflicting examples.
What is the correct htaccess for canonical www and HTTPS redirects?
This is worth getting right because sloppy versions cause redirect loops or double redirects that hurt performance and SEO, so the correct combined approach and the reasoning:
The goal and the principle: you want every visitor on one canonical version, https plus www, so a request for any of the other three combinations, http non-www, http www, https non-www, should redirect to https www in a single hop where possible, since chained redirects, http non-www to https non-www to https www, are slower and slightly dilute SEO. One rule handling both dimensions at once is the clean approach.
The combined rule in the htaccess, in the RewriteEngine context: after RewriteEngine On, a condition matching either non https or non www, then a single rule redirecting to the canonical https www with a 301. The pattern is RewriteCond checking HTTPS off OR the host not starting www, and RewriteRule sending everything to https://www.yourdomain.com with the request path preserved and an R equals 301 L flag. Using 301 makes it a permanent redirect search engines respect for canonicalization, and L stops further rule processing.
The correctness details that prevent loops: the redirect target must be the fully canonical https www URL so the redirected request itself satisfies the condition and does not redirect again, the HTTP_HOST used to preserve the domain generically or hardcoded for a single site and the rule placed before other rewrite rules so canonicalization happens first. Test all four entry combinations after deploying, http non-www, http www, https non-www and https www, confirming the first three each redirect in one hop to the fourth and the fourth does not redirect at all, the check that proves no loop and no double redirect.
The pairing with canonical tags: this htaccess redirect and the rel canonical tag work together, the redirect forcing browsers and crawlers to the canonical URL at the server level while the canonical tag declares it in the page and they should agree on the same canonical version, covered in the canonical tag thread linked alongside. Server redirect plus matching canonical tag plus a sitemap using the canonical URLs is the consistent set that makes canonicalization solid rather than half done.
The single hop rather than chained redirects point was exactly my problem, my cobbled version was doing http non-www to https non-www to https www, three hops. The combined rule fixed it to one hop, tested all four combinations as you said and they each redirect once. Paired it with matching canonical tags. Solid now.