Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save hanicker/f2eb353bb716ca4f82f9da378b46e998 to your computer and use it in GitHub Desktop.

Select an option

Save hanicker/f2eb353bb716ca4f82f9da378b46e998 to your computer and use it in GitHub Desktop.
Simple mu-plugin (move to wp-content/mu-plugins/cloudflare-flexible-ssl-redirect-loop-fix.php) to fix cloudflare redirect and optionally force https
<?php
// Fix https detection
if( (isset($_SERVER['HTTP_CF_VISITOR']) && (strpos($_SERVER['HTTP_CF_VISITOR'], 'https') !== false)) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)) ){
$_SERVER['HTTPS'] = 'on';
}
// Also force https (optional)
if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "off"){
$redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $redirect);
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment