SSL redirect on EC2 instance behind SSL terminating load balancer

.htaccessamazon-elbamazon-web-servicesssl

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,

Best Answer

untested.. but I try this

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

http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#x-forwarded-proto