Skip to content

Instantly share code, notes, and snippets.

@vastus
Last active April 6, 2024 09:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vastus/3242017a12f9277bc1b2ede13d1c46a1 to your computer and use it in GitHub Desktop.
Save vastus/3242017a12f9277bc1b2ede13d1c46a1 to your computer and use it in GitHub Desktop.
Fix too many redirects on Wordpress when serving through CloudFront
<?php
/*
* Getting too many redirects when SSL is not on and the WP_HOME and
* WP_SITEURL settings are set.
*
* These settings should work with CloudFront and a WP instance.
* You have to set custom headers for the origin for this fix to work.
*
* x-protocol: https
* x-domain-name: example.com
*
* https://www.dropbox.com/s/my8h86kkr8u1gd3/Screenshot%202018-10-24%2017.11.39.png
*/
function getOr($x, $default)
{
return isset($x) ? $x : $default;
}
$proto = getOr($_SERVER['HTTP_X_PROTOCOL'], 'http'));
$host = getOr($_SERVER['HTTP_X_DOMAIN_NAME'], $_SERVER['HTTP_HOST']);
$origin = $proto . '://' . $host;
if ($proto == 'https') {
$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS'] = 0;
}
// set the host header explicitly to $origin when using cloudfront
$ua = getOr($_SERVER['HTTP_USER_AGENT'], '');
if (stripos($ua, 'cloudfront') !== false) {
$_SERVER['HTTP_HOST'] = $host;
}
define('WP_HOME', $origin);
define('WP_SITEURL', $origin);
@jcasey5278
Copy link

How can I buy you a coffee my friend?

@vastus
Copy link
Author

vastus commented Sep 18, 2023

How can I buy you a coffee my friend?

No need! Glad you got it working.

@tux86
Copy link

tux86 commented Apr 6, 2024

It works thank you !!!!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment