Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I work around "Too many redirects error" when trying to direct all web traffic to https with ec2 apache2 ubuntu?

Here is my config file

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    Redirect permanent / https://www.mywebsite.co/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

And when I type in the browser mywebsite.co it successfully redirects me https://mywebsite.co however content does NOT render on the page because of this error (by Google Chrome)

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

I have an EC2 instance using port 80 to handle http requests and a load balancer handling https requests. I am not sure what to do and none of the solutions I've found online are working.

enter image description here

like image 474
Markus Proctor Avatar asked May 28 '16 23:05

Markus Proctor


People also ask

Why does it keep saying too many redirects occurred?

The reason you see the “too many redirects” error is because your website has been set up in a way that keeps redirecting it between different web addresses. When your browser tries to load your site, it goes back and forth between those web addresses in a way that will never complete — a redirect loop.


1 Answers

You are requesting via SSL over port 443 when you hit the ELB, but then you are requesting to the backend as insecure port 80. This is fine, but then your config has no idea that this is supposed to be over 443 because you are reverse proxying it. To the web server, this is just a port 80 insecure request, so it will try to redirect to SSL port 443, which is why it's looping.

Instead of doing a redirect, you might look at a rewrite to analyze the forwarded headers, e.g.:

RewriteEngine On
RewriteCond %{HTTP:X-FORWARDED-PORT} !=443
RewriteRule ^(.*)$ https://www.mywebsite.co$1 [R=301,NE,L]
like image 173
onetechnical Avatar answered Sep 28 '22 17:09

onetechnical