Forcing www and https with CloudFlare and NearlyFreeSpeech in one 301 redirect (rather than two)

  1. Put this in .htaccess on NearlyFreeSpeech
    RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.ericflin.com/$1 [R=301,L]
  1. On NearlyFreeSpeech, turn off canonical URLs and turn off force HTTPS. If either is forced on, requests to http://example.com get redirected to https://example.com and then to https://www.example.com, regardless of settings in .htaccess.
  1. On CloudFlare, add the following page rule:
    💡
    URL: http://*example.com/* Rule: Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://www.example.com/$2)
  1. Make sure enforce HTTPS is turned on in CloudFlare.
  1. You don't need any other rules because the .htaccess takes care of redirects from https://example.com to https://www.example.com