3
votes

Let me explain our architecture first.

www.example.com CNAME points to cloudfront distribution (d3xxxxx.cloudfront.net)

Origin for the cloudfront: route.example.com. We had select **Whitelist Headers ** as HOST in cloudfront distribution

SSL installed on Cloudfront for the domain : www.example.com

route.domain.com points to Google cloud server which has apache webserver and wordpress installed.

Issue 1. When we hit the URL www.example.com without applying SSL certificate, the home page loads www.example.com however internal links redirecting to 'route.example.com' instead of www.example.com. We believe its because of siteurl on wordpress uses route.example.com

Issue 2. After applying SSL certificate on cloudfront, when we hit the URL 'https://www.example.com', the website ended up too many redirects

We have tried the URL "WordPress + CloudFront Flexible SSL ends up in redirect loop (https)" and it doesn't seems to be helping us.

Goal: We want www.example.com to use cloudfront distribution along with SSL and doesnt want to expose webserver to enduser. The origin of the cloudfront should be route.example.com which will have wordpress application.

Any help would be appreciated.

1

1 Answers

12
votes

We had a similar problem. In our case though we weren't using a custom domain name but the CloudFront URL.

To get HTTPS to work correctly we had to do two things:

  1. Make sure the CloudFront-Forwarded-Proto header is forwarded in all cache behaviors, including the default cache behavior.

  2. Add the following code snippet to wp-config.php before require_once( ABSPATH . 'wp-settings.php' );:

    if (isset($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'])
      && $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] === 'https') {
      $_SERVER['HTTPS'] = 'on';
    }
    

The code snippet essentially tells WordPress that we're running behind a reverse proxy. This gets it to respond to HTTPS requests correctly.