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?

1 Answer 1


%{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]
  • Thanks w3d, for the solution and the clarification. As per your side issue, before this it was just a redirect from naked to www, which did work fine.
    – BernardA
    Commented Mar 20, 2015 at 19:48
  • Ah OK, you maybe alright then... I've updated the answer anyway.
    – MrWhite
    Commented Mar 20, 2015 at 19:49
  • Good that you did, you are right about the https://example.com. The second rewrite is also needed. Thanks again.
    – BernardA
    Commented Mar 20, 2015 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.