Last active
April 6, 2024 09:40
-
-
Save vastus/3242017a12f9277bc1b2ede13d1c46a1 to your computer and use it in GitHub Desktop.
Fix too many redirects on Wordpress when serving through CloudFront
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
How can I buy you a coffee my friend?
No need! Glad you got it working.
It works thank you !!!!!!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I buy you a coffee my friend?