11

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,

rix
  • 267
  • 4
  • 10

2 Answers2

17

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

Mike
  • 21,910
  • 7
  • 55
  • 79
0

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.

Jon B
  • 149
  • 7