Redirecting WordPress /.htaccess / HSTS / SSL

So, the canonical URL is https://example.com/.... (You appear to be describing a pointless intermediary redirect? You should only redirect to the final canonical URL, no need for anything in between.)

This is just a standard HTTP to HTTPS redirect.

You can do this using mod_rewrite at the top of your .htaccess file. For example:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://example.com/$1 [R=301,L]

What the above says is… if the request is for HTTP or www subdomain then 301 redirect to the canonical URL.

However, you should test this with a 302 (temporary) redirect before you implement HSTS. If you’ve already implemented HSTS and visited the HTTPS URL then the browser will automatically issue the upgraded request and you will never see the redirect (which is the whole point of HSTS).


UPDATE:

Error: HTTP does not redirect to HTTPS http://www.example.com (HTTP) redirects to https://example.com/. The first redirect from http://example.com should be to a secure page on the same host (https://www.example.com)”

Ah, it would seem that in order to satisfy the Submission Requirements to get on the HSTS preload list you must first “Redirect from HTTP to HTTPS on the same host“. (This would, however, seem to be counter-intuitive with regards to SEO and user experience.)

To split the redirect in two and redirect to HTTPS first, before the canonical www redirect then do something like the following instead:

RewriteEngine On

// 1. Redirect to HTTP to HTTPS on the same host
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

// 2. Canonical redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

For additional notes about issuing canonical redirects for the HSTS preload list see my answer to the following question on the Pro Webmasters stack:
https://webmasters.stackexchange.com/questions/112263/hsts-preload-section-on-htaccess

Leave a Comment