// 判断协议安全 $schema = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http'; // 当前访问 host 和路径 $host = $_SERVER['HTTP_HOST'] ?? ''; $requestUri = $_SERVER['REQUEST_URI'] ?? '/'; // 如果不带 www,跳到 www,保留路径 + 参数 if (strpos($host, 'www.') !== 0) { $redirectUrl = 'https://www.' . str_replace('www.', '', $host) . $requestUri; header("Location: $redirectUrl", true, 301); exit; } // 跳转逻辑 $domainFile = __DIR__ . '/domains.txt'; $urls = file($domainFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!$urls) { http_response_code(500); echo '跳转失败,域名列表为空'; exit; } $target = rtrim($urls[array_rand($urls)], '/') . $requestUri; header("Location: $target", true, 301); exit;