I'm trying to redirect all url's to https in a web application.

I have an ec2 instance behind an elastic load balancer. SSL is terminated on the load balancer.

Any attempts at redirection end up giving me the familiar 'this page is requesting in a way which will never complete'.

The load balancer forwards 443 and 80 to port 80 on the instance.

This is what I have in my .htaccess.

RewriteCond %{X-FORWARDED-PROTO} !=on  
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Does anyone have any idea how to accomplish this? Kind thanks,


untested.. but I try this

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]



Mike's answer is mostly correct, but it's important to point out that the load balancer will still need to carryout its health check routine to ensure that the ec2 instance is still alive and well. This can be done with an additional RewriteCond

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/alive\.html$
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

...where alive.html is the name of your health check file.

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.