Force HTTPS with AWS Elastic load balancer
So I assume you have the ELB set to accept traffic on both HTTP/80 and HTTPS/443, but port-forward all to HTTP.
If you want to use your method (which is clever), are you sure what you get isn't %{X-Forwarded-Proto}
-- the HTTP:
prefix looks odd to me. Other than that, this looks right to me.
If this is in a virtual host or the main server config and it's still not working, you can add
RewriteLog rewrite-log
RewriteLogLevel 3
then look in the file rewrite-log
to see what's actually going on. This file can be incredibly verbose, start with lower levels. Logging cannot be initiated from .htaccess
files.
(I would suggest you make the flags on the RewriteRule
[R=301,L]
which will cause the server to send back a 301 response, which tells search engines to consider the redirect permanent, and update its links accordingly.)
Just wanted to add my experience as I struggled for hours before realizing that my heartbeat file had an unescaped dot in it (/alive.html). Duh.
The second problem was that the main domain was not redirecting, but files were. So http://domain.com/hello.html was redirecting to https://domain.com/hello.html, but http://domain.com was not.
Here's what I put in my .htaccess file that worked for me:
RewriteEngine On
# SSL connection forced
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/alive\.html$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
In my case the problem turned out to be that the !https condition was breaking the health check.
changing the condition to ^http$ made it work
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
found that here: https://forums.aws.amazon.com/thread.jspa?messageID=641930