Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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,

share|improve this question
up vote 17 down vote accepted

untested.. but I try this

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

share|improve this answer
Yes! Thank you so much. Been struggling with this for a while now. Much appreciated. Please upvote this is answer if you find this a help. Thank you... – rix May 4 '12 at 14:39
the link is broken – BillR Mar 13 '13 at 17:14
Solution still works. Some explanation could be found here:… – Tim Apr 16 '13 at 7:21
Worked for me. Tested today – Ben Sinclair Sep 23 '14 at 6:46

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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