Fixing WordPress Redirect Loop with AWS Elastic Load Balancer

Share This Post:

Fixing WordPress Redirect Loop with AWS Elastic Load Balancer

Recently we were struggling with a problem - we were trying to get SSL working with a WordPress website running on Elastic Beanstalk, Cloudflare, and an Amazon-issued certificate for the elastic load balancer. When we set the siteurl and home options in the wp_options table to use https, most of the site worked, but we couldn't login to the website using at either wp-admin or wp-login.php - every time we tried to access that page, we would get an "err_too_many_redirects" error in Chrome. Changing the wp_options values to http allowed wp-admin and wp-login.php to load, but since we are forcing https, we got mixed content errors.

The solution, it turns out, is related to the fact that AWS Elastic Load Balancers communicate with EC2 instances over standard HTTP. This is transparent to the end-user, and is completely secure since the AWS security policies that manage communications between the ELB and EC2 instances prevent even other EC2 instances from listening in. The solution was as simple as adding this to our wp-config.php file:

$_SERVER['HTTPS'] = 'on';

We were migrating from a single-server without a load balancer, and this particular PHP code is not required for most WordPress instances served over SSL; however, due to the ELB configuration, it is required here. With that single line of code, our https connection to wp-admin and wp-login.php worked without any mixed content errors.

Posted by Lance Johnson