/
www
/
Upload File
HOME
#!/usr/bin/env php <?php /** * 通用 Sitemap 生成脚本 * 扫描网站目录,生成包含所有 HTML/PHP 文件的 sitemap.xml */ // 配置 $siteConfig = [ 'shgd123.com' => [ 'url' => 'https://www.shgd123.com', 'path' => '/www/wwwroot/shgd123.com', 'exclude' => ['data', 'include', 'gyxrfj', 'fd', 'install', 'backup'] ], 'rfj123.com' => [ 'url' => 'https://www.rfj123.com', 'path' => '/www/wwwroot/rfj123.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], 'gaowenlu123.com' => [ 'url' => 'https://www.gaowenlu123.com', 'path' => '/www/wwwroot/gaowenlu123.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], 'gyhx123.com' => [ 'url' => 'https://www.gyhx123.com', 'path' => '/www/wwwroot/gyhx123.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], 'uvghj.com' => [ 'url' => 'https://www.uvghj.com', 'path' => '/www/wwwroot/uvghj.com', 'exclude' => ['data', 'include', 'install', 'backup'] ], 'm.shgd123.com' => [ 'url' => 'https://m.shgd123.com', 'path' => '/www/wwwroot/m.shgd123.com', 'exclude' => ['data', 'runtime', 'config', 'backup'] ], 'tzsb168.com' => [ 'url' => 'https://www.tzsb168.com', 'path' => '/www/wwwroot/tzsb168.com', 'exclude' => ['data', 'runtime', 'config', 'backup'] ] ]; // 生成 sitemap function generateSitemap($config) { $files = []; $visited = []; // 递归扫描目录 function scanDirectory($dir, $baseUrl, $basePath, $exclude, &$files, &$visited) { $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, $files, $visited); } 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($config['path'], $config['url'], $config['path'], $config['exclude'], $files, $visited); // 生成 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 = $config['path'] . '/sitemap.xml'; file_put_contents($sitemapFile, $xml); return [ 'file' => $sitemapFile, 'count' => count($files), 'url' => $config['url'] . '/sitemap.xml' ]; } // 执行生成 echo "开始生成 Sitemap...\n"; foreach ($siteConfig as $site => $config) { echo "\n处理 $site..."; $result = generateSitemap($config); echo " 生成成功,包含 {$result['count']} 个链接"; echo " Sitemap: {$result['url']}"; } echo "\n\n所有 Sitemap 生成完成!\n"; ?>