diff --git a/crm/Core/View.php b/crm/Core/View.php index 934efd7..fa574e3 100644 --- a/crm/Core/View.php +++ b/crm/Core/View.php @@ -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; }