/
www
/
Upload File
HOME
#!/usr/bin/env php <?php /** * 通用 Sitemap 生成脚本 - 修复版本 * 扫描网站目录,生成包含所有 HTML/PHP 文件的 sitemap.xml */ // 配置 $sites = [ [ 'name' => 'shgd123.com', 'url' => 'https://www.shgd123.com', 'path' => '/www/wwwroot/shgd123.com', 'exclude' => ['data', 'include', 'gyxrfj', 'fd', 'install', 'backup'] ], [ 'name' => 'rfj123.com', 'url' => 'https://www.rfj123.com', 'path' => '/www/wwwroot/rfj123.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], [ 'name' => 'gaowenlu123.com', 'url' => 'https://www.gaowenlu123.com', 'path' => '/www/wwwroot/gaowenlu123.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], [ 'name' => 'gyhx123.com', 'url' => 'https://www.gyhx123.com', 'path' => '/www/wwwroot/gyhx123.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], [ 'name' => 'uvghj.com', 'url' => 'https://www.uvghj.com', 'path' => '/www/wwwroot/uvghj.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], [ 'name' => 'm.shgd123.com', 'url' => 'https://m.shgd123.com', 'path' => '/www/wwwroot/m.shgd123.com', 'exclude' => ['data', 'runtime', 'config', 'backup'] ], [ 'name' => 'tzsb168.com', 'url' => 'https://www.tzsb168.com', 'path' => '/www/wwwroot/tzsb168.com', 'exclude' => ['data', 'runtime', 'config', 'backup'] ] ]; // 生成单个站点的 sitemap function generateSingleSitemap($site) { $files = []; $visited = []; // 内部递归函数 $scanDirectory = function($dir, $baseUrl, $basePath, $exclude) use (&$files, &$visited, &$scanDirectory) { $handle = opendir($dir); if (!$handle) return; while (false !== ($entry = readdir($handle))) { if ($entry == '.' || $entry == '..') continue; $fullPath = $dir . '/' . $entry; $relativePath = str_replace($basePath, '', $fullPath); // 检查是否在排除列表中 $shouldExclude = false; foreach ($exclude as $pattern) { if (strpos($relativePath, '/' . $pattern . '/') !== false || $entry == $pattern) { $shouldExclude = true; break; } } if ($shouldExclude) continue; if (is_dir($fullPath)) { $scanDirectory($fullPath, $baseUrl, $basePath, $exclude); } elseif (preg_match('/\.(html|php|htm)$/', $entry)) { $url = $baseUrl . $relativePath; $url = str_replace('//', '/', $url); $url = str_replace(':/', '://', $url); if (!in_array($url, $visited)) { $visited[] = $url; $files[] = [ 'url' => $url, 'lastmod' => date('Y-m-d', filemtime($fullPath)), 'changefreq' => 'weekly', 'priority' => 0.8 ]; } } } closedir($handle); }; $scanDirectory($site['path'], $site['url'], $site['path'], $site['exclude']); // 生成 XML $xml = '<?xml version="1.0" encoding="UTF-8"?> '; $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> '; foreach ($files as $file) { $xml .= ' <url> '; $xml .= ' <loc>' . htmlspecialchars($file['url']) . '</loc> '; $xml .= ' <lastmod>' . $file['lastmod'] . '</lastmod> '; $xml .= ' <changefreq>' . $file['changefreq'] . '</changefreq> '; $xml .= ' <priority>' . $file['priority'] . '</priority> '; $xml .= ' </url> '; } $xml .= '</urlset>'; // 保存到文件 $sitemapFile = $site['path'] . '/sitemap.xml'; file_put_contents($sitemapFile, $xml); return [ 'file' => $sitemapFile, 'count' => count($files), 'url' => $site['url'] . '/sitemap.xml' ]; } // 执行生成 echo "开始生成 Sitemap...\n"; foreach ($sites as $site) { echo "\n处理 {$site['name']}..."; try { $result = generateSingleSitemap($site); echo " 生成成功,包含 {$result['count']} 个链接"; echo " Sitemap: {$result['url']}"; } catch (Exception $e) { echo " 生成失败: " . $e->getMessage(); } } echo "\n\n所有 Sitemap 生成完成!\n"; ?>