fix(view): respect APP_URL and X-Forwarded-* in getBaseUrl() — BIT-24927 #2
+19
-6
@@ -278,18 +278,31 @@ class View
|
||||
*/
|
||||
public static function getBaseUrl(): string
|
||||
{
|
||||
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
|
||||
// Use explicit APP_URL when set (required for reverse-proxy deployments)
|
||||
$appUrl = $_ENV['APP_URL'] ?? '';
|
||||
if ($appUrl && filter_var($appUrl, FILTER_VALIDATE_URL)) {
|
||||
return rtrim($appUrl, '/');
|
||||
}
|
||||
|
||||
// Detect scheme: check X-Forwarded-Proto first (set by reverse proxy)
|
||||
$forwardedProto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
|
||||
if ($forwardedProto === 'https') {
|
||||
$protocol = 'https';
|
||||
} else {
|
||||
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https' : 'http';
|
||||
}
|
||||
|
||||
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
|
||||
|
||||
|
||||
// If HTTP_HOST already includes the port, use it as is
|
||||
if (strpos($host, ':') !== false) {
|
||||
return $protocol . '://' . $host;
|
||||
}
|
||||
|
||||
// Otherwise, add port if needed
|
||||
$port = $_SERVER['SERVER_PORT'] ?? '';
|
||||
|
||||
// Use X-Forwarded-Port if available, otherwise fall back to SERVER_PORT
|
||||
$port = $_SERVER['HTTP_X_FORWARDED_PORT'] ?? $_SERVER['SERVER_PORT'] ?? '';
|
||||
$port = ($port === '80' || $port === '443') ? '' : ':' . $port;
|
||||
|
||||
|
||||
return $protocol . '://' . $host . $port;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user