From 58b875e8ab7366de5793aaf2166c8641ca761943 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Tue, 15 Jul 2025 10:55:16 -0500 Subject: [PATCH 1/6] Add enforceDomain() method. --- README.md | 5 +++++ src/Environment.php | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 59af819..50b8843 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,11 @@ elseif (Environment::isLocal()) { // Set some development environment settings overrides. } +// Redirect any internal platform domains to a preferred domain. +if (Environment::isProduction()) { + Environment::enforceDomain('www.example.com'); +} + // Include a environment-specific settings file. if ($environment = Environment::getEnvironment()) { $settings_file = 'settings.' . $environment . '.php'; diff --git a/src/Environment.php b/src/Environment.php index eaf291a..b07bd61 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -180,4 +180,26 @@ public static function getComposerLockFilename(): string $filename = static::getComposerFilename(); return pathinfo($filename, PATHINFO_FILENAME) . '.lock'; } + + /** + * Redirect requests to a preferred domain. + * + * This will not redirect CLI requests. + * + * @param string $domain + * The preferred domain name. + */ + public static function enforceDomain(string $domain): void + { + if (PHP_SAPI !== 'cli' && $_SERVER['HTTP_HOST'] !== $domain) { + // Name transaction "redirect" in New Relic for improved reporting. + if (extension_loaded('newrelic')) { + newrelic_name_transaction('redirect'); + } + + header('HTTP/1.0 301 Moved Permanently'); + header('Location: https://' . $domain . $_SERVER['REQUEST_URI']); + exit(); + } + } } From 9e9c1b56447c5da37ca78176c140416ef9fddc80 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Tue, 15 Jul 2025 11:12:40 -0500 Subject: [PATCH 2/6] Add getCurrentDomain() helper. --- src/Environment.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Environment.php b/src/Environment.php index b07bd61..94180ca 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -181,6 +181,17 @@ public static function getComposerLockFilename(): string return pathinfo($filename, PATHINFO_FILENAME) . '.lock'; } + /** + * Get the current domain name. + * + * @return string + * The current domain name. + */ + public static function getCurrentDomain(): string + { + return $_SERVER['HTTP_X_FORWARDED_HOST'] ?? $_SERVER['HTTP_HOST']; + } + /** * Redirect requests to a preferred domain. * @@ -191,7 +202,7 @@ public static function getComposerLockFilename(): string */ public static function enforceDomain(string $domain): void { - if (PHP_SAPI !== 'cli' && $_SERVER['HTTP_HOST'] !== $domain) { + if (PHP_SAPI !== 'cli' && static::getCurrentDomain() !== $domain) { // Name transaction "redirect" in New Relic for improved reporting. if (extension_loaded('newrelic')) { newrelic_name_transaction('redirect'); From c552ed4422888fbd1031d88fb46b48c31f3be875 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Thu, 17 Jul 2025 09:10:37 -0500 Subject: [PATCH 3/6] Use isCli() method. --- src/Environment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Environment.php b/src/Environment.php index 94180ca..2cb999d 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -202,7 +202,7 @@ public static function getCurrentDomain(): string */ public static function enforceDomain(string $domain): void { - if (PHP_SAPI !== 'cli' && static::getCurrentDomain() !== $domain) { + if (!static::isCli() && static::getCurrentDomain() !== $domain) { // Name transaction "redirect" in New Relic for improved reporting. if (extension_loaded('newrelic')) { newrelic_name_transaction('redirect'); From 8618784878350190a0fd203ea418a8e9782375b1 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Thu, 17 Jul 2025 09:15:22 -0500 Subject: [PATCH 4/6] use getCurrentDomain() from isCustomDomain(). --- src/Pantheon.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pantheon.php b/src/Pantheon.php index 43f4eee..26273aa 100644 --- a/src/Pantheon.php +++ b/src/Pantheon.php @@ -65,6 +65,6 @@ public static function isMultidev(): bool */ public static function isCustomDomain(): bool { - return isset($_SERVER['HTTP_HOST']) && !str_ends_with($_SERVER['HTTP_HOST'], static::PLATFORM_DOMAIN); + return !str_ends_with(Environment::getCurrentDomain(), static::PLATFORM_DOMAIN); } } From f36dc8b50411be81cda2ad8b6b8f6015321a6590 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Thu, 17 Jul 2025 09:33:36 -0500 Subject: [PATCH 5/6] Add todo about improving getCurrentDomain(). --- src/Environment.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Environment.php b/src/Environment.php index 2cb999d..6911335 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -186,6 +186,8 @@ public static function getComposerLockFilename(): string * * @return string * The current domain name. + * + * @todo This could be improved. See https://stackoverflow.com/questions/1459739. */ public static function getCurrentDomain(): string { From 3210bde8d2e5eb51333e8d03e8d87a39830f2802 Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Thu, 17 Jul 2025 09:35:39 -0500 Subject: [PATCH 6/6] Add static caching for host. --- src/Environment.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Environment.php b/src/Environment.php index 6911335..2f51cdc 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -191,7 +191,11 @@ public static function getComposerLockFilename(): string */ public static function getCurrentDomain(): string { - return $_SERVER['HTTP_X_FORWARDED_HOST'] ?? $_SERVER['HTTP_HOST']; + static $host; + if (!isset($host)) { + $host = $_SERVER['HTTP_X_FORWARDED_HOST'] ?? $_SERVER['HTTP_HOST']; + } + return $host; } /**