1

I need to force all traffic for all pages on example.com to https://www.example.com . After much googles, and hosting site help docs, the consensus (and what my hosting - BlueHost - docs say to do) is this:

RewriteEngine on
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R]

Which seems like it should work. But when I type in just example.com in a browser (FF or Chrome), the request is redirected to

https://www.example.com/https:/www.example.com/

The rule is at the top of the htaccess file, so don't understand why this is happening.

1
RewriteRule ^(.*)$ https://www.example.com/$1 [R]

You probably need the L (last) flag. ie. [R,L]. Otherwise, with this directive being at the top of the .htaccess file, processing will continue through the file and it's likely to be rewritten again - so literally anything could happen. (I would be surprised if the Bluehost docs did not include this?)

Also, once you have confirmed this is working OK, you should change this to a permanent (301) redirect. ie. [R=301,L].

  • Thanks. So (to help with my understanding), the "L" flag will stop processing of htaccess, and the RewriteRule will initiate another request, which will cause the htaccess rule to be accessed again (assuming there is more to the htaccess file, which there is). The "L" flag will stop any further htaccess rule processing when the condition is met for that RewriteRule. Am I getting closer to correct understanding? – Rick Hellewell Jul 5 '17 at 0:17
  • Yes, something like that (I think)... In .htaccess the L flag stops the current pass through the file. Since a Redirect has been set, Apache now sends a redirect (302) response back to the client (browser). (First request ends here.) The client then sends a second request, this time for https://.... Since the (second) request is now port 443 (ie. HTTPS) the first rule does not match. Processing then continues through the .htaccess file. – MrWhite Jul 5 '17 at 0:44
  • Note that the L flag does not stop all processing of the .htaccess file. The process effectively "starts over" (unless a redirect is triggered) and will continue to loop until the URL passes through unchanged. – MrWhite Jul 5 '17 at 0:47
  • I think that works, but there is the additional problem of redirecting the domain to a folder (the domain points to the public_html folder, but the root content 'lives' in a public_html/somefolder folder. The combination of forcing https and also redirecting the domain to the somefolder folder is not working, using the syntax as provided by the host. Since it is a different question, I'm creating a new one, rather than editing this one - and since your answer is correct for the original question. Thanks. – Rick Hellewell Jul 5 '17 at 2:23
  • Whether the site is serving files from a subdirectory or not should be unrelated to this canonical HTTP to HTTPS redirect. I'll have a look at your new question. – MrWhite Jul 5 '17 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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