// ============================================================ // 🔥 强制所有404显示 404.php(放在最前面) // ============================================================ $requestUri = $_SERVER['REQUEST_URI']; $ext = pathinfo($requestUri, PATHINFO_EXTENSION); $staticExtensions = ['jpg', 'jpeg', 'png', 'gif', 'ico', 'css', 'js', 'svg', 'woff', 'woff2', 'ttf', 'eot', 'webp', 'mp4', 'webm']; // 如果不是静态文件,且不是存在的PHP文件 if (!in_array(strtolower($ext), $staticExtensions)) { $filePath = __DIR__ . $requestUri; if (!file_exists($filePath) || is_dir($filePath)) { // 但不要拦截正常的PHP文件 if (strpos($requestUri, '.php') === false) { http_response_code(404); include __DIR__ . '/404.php'; exit; } } } // **引入配置和依赖** include('config.php'); require 'vendor/autoload.php'; use Elastic\Elasticsearch\ClientBuilder; // **获取请求的主机名和请求路径** $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost'; $requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; $requestPath = parse_url($requestUri, PHP_URL_PATH) ?? '/'; // **连接 Redis** $redis = new Redis(); try { $redis->connect('127.0.0.1', 6379); } catch (RedisException $e) { error_log('Redis 连接失败: ' . $e->getMessage()); $redis = null; } // **定义域名绑定的 TXT 文件** $domainsFile = __DIR__ . '/domains.txt'; // **获取当前域名(保留子域名)** $currentDomain = $host; function resolveTemplateConfig($domain, $domainsFile) { // 如果 domains.txt 不存在,直接返回空(让调用方处理) if (!file_exists($domainsFile)) { return null; } $lines = file($domainsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { $parts = explode('|', $line); if (count($parts) === 3 && trim($parts[0]) === $domain) { return [ 'template_dir' => 'template/' . trim($parts[1]), 'site_name' => trim($parts[2]) ]; } } $domainParts = explode('.', $domain); if (count($domainParts) > 2) { $mainDomain = implode('.', array_slice($domainParts, -2)); } else { $mainDomain = $domain; } $mainDomain = preg_replace('/^www\./', '', $mainDomain); foreach ($lines as $line) { $parts = explode('|', $line); if (count($parts) === 3 && trim($parts[0]) === $mainDomain) { return [ 'template_dir' => 'template/' . trim($parts[1]), 'site_name' => trim($parts[2]) ]; } } return null; } $config = resolveTemplateConfig($currentDomain, $domainsFile); $templateDir = $config['template_dir']; $siteName = $config['site_name']; if (empty($requestPath)) { $requestPath = '/'; } $GLOBALS['siteName'] = $siteName; $GLOBALS['templateDir'] = $templateDir; // **检查域名是否在 domains.txt 中存在,如果不存在则重定向** function redirectIfDomainNotFound($domain, $domainsFile) { // 读取 domains.txt 文件 $lines = file($domainsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $foundDomains = []; foreach ($lines as $line) { $parts = explode('|', $line); if (count($parts) === 3) { // 收集所有存在的域名 $foundDomains[] = trim($parts[0]); } } // 如果没有找到当前域名,随机选择一个存在的域名进行重定向 if (!in_array($domain, $foundDomains)) { $randomDomain = $foundDomains[array_rand($foundDomains)]; $redirectUrl = "http://www.{$randomDomain}{$_SERVER['REQUEST_URI']}"; header("Location: $redirectUrl"); exit; } } // **检查当前域名是否存在,如果不存在则重定向** redirectIfDomainNotFound($mainDomain, $domainsFile); // ============================================================ // ========== 动态路由前缀函数 ========== // ============================================================ function buildRoutePrefix($domain, $type = 'detail') { $domain = preg_replace('/^www\./', '', $domain); $parts = explode('.', $domain); if (count($parts) > 2) { $mainDomain = implode('.', array_slice($parts, -2)); $mainLabel = explode('.', $mainDomain)[0]; } else { $mainLabel = $parts[0]; } $mainLabel = preg_replace('/[^a-z0-9]/', '', $mainLabel); $prefix = substr($mainLabel, 0, 4); if ($prefix === '') $prefix = 'vod'; switch ($type) { case 'detail': return $prefix . 'de'; case 'category': return $prefix . 'ry'; case 'play': return $prefix . 'pf'; case 'tag': return $prefix . 'tag'; default: return $prefix; } } $detailPrefix = buildRoutePrefix($mainDomain, 'detail'); // smbtde $categoryPrefix = buildRoutePrefix($mainDomain, 'category'); // smbtry $playPrefix = buildRoutePrefix($mainDomain, 'play'); // smbtpf $tagPrefix = buildRoutePrefix($mainDomain, 'tag'); // smbttag // ============================================================ // **使用 switch 进行路由匹配** // ============================================================ switch (true) { // ========== 🔥 标签页 - 放在最前面 ========== case preg_match('/^\/tag\/([a-zA-Z0-9]+)\.html$/', $requestPath, $matches): $_GET['keyword'] = $matches[1]; include 'tag.php'; exit; case preg_match('/^\/' . preg_quote($tagPrefix, '/') . '\/([a-zA-Z0-9]+)\.html$/', $requestPath, $matches): $_GET['keyword'] = $matches[1]; include 'tag.php'; exit; // ========== 🔥 动态路由 ========== // 详情页 case preg_match('/^\/' . preg_quote($detailPrefix, '/') . '\/([0-9]+)\.html$/', $requestPath, $matches): $_GET['vod_id'] = $matches[1]; include 'detail.php'; exit; // 播放页 case preg_match('/^\/' . preg_quote($playPrefix, '/') . '\/([0-9]+)-([0-9]+)-([0-9]+)\.html$/', $requestPath, $matches): $_GET['vod_id'] = $matches[1]; $_GET['player_id'] = $matches[2]; $_GET['episode_id'] = $matches[3]; include 'play.php'; exit; case preg_match('/^\/' . preg_quote($playPrefix, '/') . '\/([0-9]+)\.html$/', $requestPath, $matches): $_GET['vod_id'] = $matches[1]; include 'play.php'; exit; // 分类页 case preg_match('/^\/' . preg_quote($categoryPrefix, '/') . '\/([0-9]+)\.html$/', $requestPath, $matches): $_GET['type_id'] = $matches[1]; $_GET['page'] = 1; include 'category.php'; exit; case preg_match('/^\/' . preg_quote($categoryPrefix, '/') . '\/([0-9]+)\/([0-9]+)\.html$/', $requestPath, $matches): $_GET['type_id'] = $matches[1]; $_GET['page'] = intval($matches[2]); include 'category.php'; exit; // ========== 🔥 子域名降级 ========== case preg_match('/^([^.]+)\.(.+)$/', $host, $subdomainMatches) && $subdomainMatches[1] !== 'www': $hostParts = explode('.', $host); $hostCount = count($hostParts); $leftmost = $hostParts[0]; // 白名单:二级子域名走首页 $whitelist = ['app', 'm', 'mp', 'web', 'h5', 'wap', 'mip']; // 只有二级子域名(hostCount === 3)且在白名单中才走首页 if ($hostCount === 3 && in_array($leftmost, $whitelist)) { include __DIR__ . '/home.php'; exit; } // 其他所有子域名(包括三级子域名)走 fjx.php $_GET['keyword'] = $leftmost; include __DIR__ . '/fjx.php'; exit; // ========== 首页 ========== case ($requestPath === '/' || $requestPath === ''): include __DIR__ . '/home.php'; exit; // ========== 搜索页 ========== case preg_match('/^\/search\/([^\/]+)\/([0-9]+).html$/', $requestPath, $matches): $_GET['keyword'] = urldecode($matches[1]); $_GET['page'] = intval($matches[2]); include 'search.php'; exit; case preg_match('/^\/search\/(.+).html$/', $requestPath, $matches): $_GET['keyword'] = urldecode($matches[1]); $_GET['page'] = 1; include 'search.php'; exit; // ========== 404 ========== default: http_response_code(404); include __DIR__ . '/template/404.php'; exit; }