Last active
July 25, 2019 02:48
-
-
Save dtateii/1b245966d9b4835e73db9a013c75090a to your computer and use it in GitHub Desktop.
Pantheon HTTPS and www hostname redirects for WordPress config
This file contains hidden or 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 | |
// wp-config.php snippet. Put this just before the "Happing Pressing!" comment. | |
// HTTPS and www redirects. | |
// Edit constants below based on your needs. | |
define( 'HOSTNAME_PREFERRED', 'www.domain.com' ); | |
define( 'HOSTNAME_PANTHEON_LIVE', 'live-domain.pantheonsite.io' ); | |
define( 'FORCE_HTTPS', true ); | |
// Probably don't edit this part, uses the constants to do expected things. | |
if ( isset( $_SERVER['PANTHEON_ENVIRONMENT'] ) && | |
php_sapi_name() !== 'cli' ) { | |
// Force HTTPS on for ALL Pantheon Environments. | |
if ( FORCE_HTTPS && ( ! isset( $_SERVER['HTTP_X_SSL'] ) && 'https' !== $_SERVER['HTTP_X_FORWARDED_PROTO'] ) || | |
( isset( $_SERVER['HTTP_X_SSL'] ) && 'ON' !== $_SERVER['HTTP_X_SSL'] ) ) { | |
header( 'HTTP/1.0 301 Moved Permanently' ); | |
header( 'Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); | |
exit(); | |
} | |
// Force preferred hostname for live/prod site, except the Pantheon | |
// default hostname -- leave that working in case of emergency. | |
if ( 'live' === $_SERVER['PANTHEON_ENVIRONMENT'] ) { | |
if ( HOSTNAME_PREFERRED !== $_SERVER['HTTP_HOST'] && | |
HOSTNAME_PANTHEON_LIVE !== $_SERVER['HTTP_HOST'] ) { | |
if ( FORCE_HTTPS ) { | |
$scheme = 'https://'; | |
} else { | |
$scheme = 'http://'; | |
} | |
$location = $scheme . HOSTNAME_PREFERRED . $_SERVER['REQUEST_URI']; | |
// Issue the redirect. | |
header( 'HTTP/1.0 301 Moved Permanently' ); | |
header( "Location: $location" ); | |
exit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment