Fix AWS Redirecting to SSL ERR_TOO_MANY_REDIRECTS

amazon-web-services

Needing to redirect to https. The same (in root (html directory)) works on other servers, but not on AWS (.htaccess):

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://there.com/$1 [R,L]

Load Balancer (type:application) in Listeners is:

HTTP : 80, View/edit rules > HTTPS:80 > IF Requests otherwise not routed THEN Forward to service-http

HTTPS : 443, View/edit rules > HTTPS:443 > IF Requests otherwise not routed THEN Forward to service-http.

In etc/httpd/conf/httpd.conf there's nothing on VirtualHost *:80 or VirtualHost *:443

If I omit the .htaccess rules then it's not rerouting to https.

Where else should I look?

PS. " Not working" means "ERR_TOO_MANY_REDIRECTS".

Best Answer

Correct me if I'm wrong - you've got an ALB Listener that listens on HTTP (80) and HTTPS (443) and in both cases forwards the request to the backend over HTTP (80), correct?

If that's the case the backend sees SERVER_PORT=80 in both cases, because that's how the load balancer talks to it. There is no HTTPS between the load balancer and the backend, hence the port is always 80 as far as the backend is concerned.

To make it work you'll have to inspect X-Forwarded-Proto HTTP header and use that in the RewriteCond like this:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Hope that helps :)

Related Topic