https redirects + Cloudflare Page Rules missing situation
I have just moved my website to https and after some scares it is working, mostly.
I did find out that when I input the naked domain on the browser it is not going to the desired URL, as follows:
- typed URL =
example.com - redirects to =
https://example.com - desired URL =
https://www.example.com
The following Page Rule is set on Cloudflare:
- pattern =
http://*example.com/* - rule = Forwarding to
https://www.example.com/$2
On the origin server, the .htaccess file has:
RewriteCond %HTTPS off
RewriteRule (.*) https://%HTTP_HOST%REQUEST_URI [R=301,L]
What am I missing?
%HTTP_HOST will refer to whatever host the site was accessed through. ie. example.com or www.example.com. To always redirect to www.example.com then simply put this in the substitution:
RewriteCond %HTTPS off
RewriteRule .* https://www.example.com%REQUEST_URI [R=301,L]
But unless you had a similar rule before then both example.com and www.example.com would have been accessible (and possibly indexed). In fact, if you access https://example.com now it does not redirect to https://www.example.com. You also need the following, in addition to the above:
RewriteCond %HTTP_HOST !www.
RewriteRule .* https://www.%HTTP_HOST%REQUEST_URI [R=301,L]
So, in summary, the first block corrects for HTTP -> HTTPS and the second corrects if the www subdomain is omitted. I also removed the parentheses in your pattern (.*) since you aren't capturing it for the substitution.
These two rule blocks could be combined into one if desired...
RewriteCond %HTTPS off [OR]
RewriteCond %HTTP_HOST !www.
RewriteRule .* https://www.example.com%REQUEST_URI [R=301,L]
Comments
Post a Comment