1

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".

  • Not sure specifically for ELB, but is this Apache config you have shown on a server behind a load balancer or similar rather than the balancer itself? Is the load balancer performing TLS termination for you? – Fire Lancer Sep 28 '18 at 17:52
  • Not very familiar with AWS or, to tell the truth, server management in general. How can I find it out? – Konservin Sep 28 '18 at 17:55
  • Are you by any chance using wordpress? I've seen this happen most often with wordpress. If so, change the HOME and SITEURL entries in the mysql database to match the address with https://, otherwise wordpress will continually redirect to http://, then the htaccess rules will redirect to https://, in a loop. – Ben M. Sep 28 '18 at 18:08
  • No wordpress, sorry. MODX yes, but it's in a subdirectory and shouldn't interfere with .htaccess in root (html) dir. – Konservin Sep 28 '18 at 18:19
  • Well if you added an ELB/Load Balancer I guess you would know. The general thing is if there is a balancer or such doing TLS termination (so it can inspect/log https URL's, headers etc.) then it will normally forward to the web server over http not https (should be a secure private network, it also likely adds some X-Forwarded-* headers). If your web server then tries to redirect that http to https (which was actually https on the client), it will infinitely loop. After a few rounds the browser gives up with a "too many redirect" type error. – Fire Lancer Sep 28 '18 at 18:59
2

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 :)

| improve this answer | |
  • Cannot correct you, as you're not wrong. Worked as a charm. Thanks for explanation, too. – Konservin Sep 29 '18 at 6:24

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.