<?php
$is_https = false;

// 1. Pengecekan HTTPS (Cloudflare Tunnel biasanya membawa header ini)
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') {
    $is_https = true;
} elseif (isset($_SERVER['HTTP_CF_VISITOR'])) {
    $cf_visitor = json_decode($_SERVER['HTTP_CF_VISITOR']);
    if ($cf_visitor && isset($cf_visitor->scheme) && $cf_visitor->scheme === 'https') {
        $is_https = true;
    }
} elseif (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on') {
    $is_https = true;
}

$protocol = $is_https ? 'https://' : 'http://';

// 2. Ambil Host Asli
// CF Tunnel terkadang menyimpan domain publik asli di HTTP_X_FORWARDED_HOST
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
    $raw_host = $_SERVER['HTTP_X_FORWARDED_HOST'];
} else {
    $raw_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
}

// 3. Hapus Port Lokal (8085) dari Host
// Memecah "domain.com:8085" menjadi "domain.com"
$host = explode(':', $raw_host)[0];

$target_path = '/live/simrs';

// 4. Eksekusi Redirect
header("Location: " . $protocol . $host . $target_path, true, 301);
exit();
?>
