Create sitemap

This commit is contained in:
Bruno Rybársky 2024-02-15 10:39:02 +01:00
parent 051ca0d101
commit e37387f084

@ -7,8 +7,12 @@ function generateSitemap(): string {
$site_dirs = array_diff(scandir($routerConfig["page_dir"]), array('.', '..')); $site_dirs = array_diff(scandir($routerConfig["page_dir"]), array('.', '..'));
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$domain = $_SERVER['HTTP_HOST'];
$subdomain = ""; // You may need to modify this based on your subdomain logic
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$sitemap .= '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL; $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
foreach ($site_dirs as $site_dir) { foreach ($site_dirs as $site_dir) {
$pages_dir = array_diff(scandir($routerConfig["page_dir"] . $site_dir), array('.', '..')); $pages_dir = array_diff(scandir($routerConfig["page_dir"] . $site_dir), array('.', '..'));
@ -17,7 +21,8 @@ function generateSitemap(): string {
$page_file_tmp = explode(".", $page_file); $page_file_tmp = explode(".", $page_file);
$page_basename = $page_file_tmp[0]; $page_basename = $page_file_tmp[0];
$page_file_path = $routerConfig["page_dir"] . $site_dir . "/" . $page_file; $page_file_path = $routerConfig["page_dir"] . $site_dir . "/" . $page_file;
$page_location = "/" . $site_dir . "/" . $page_basename;
$page_location = $protocol . "://" . $subdomain . $domain . "/" . $site_dir . "/" . $page_basename;
if ($page_file_tmp[1] == "html") { if ($page_file_tmp[1] == "html") {
$page_tmp = file_get_contents($page_file_path); $page_tmp = file_get_contents($page_file_path);