use Spatie\Sitemap\SitemapGenerator; use Spatie\Sitemap\Tags\Url; Route::get('/sitemap.xml', function () { $baseUrl = 'https://chcsaude.org'; $sitemapPath = public_path('sitemap.xml'); SitemapGenerator::create($baseUrl) ->hasCrawled(function (Url $url) use ($baseUrl) { $absoluteUrl = (string) $url->url; $parts = parse_url($absoluteUrl); $scheme = $parts['scheme'] ?? 'https'; $host = strtolower($parts['host'] ?? ''); $path = $parts['path'] ?? '/'; $query = $parts['query'] ?? null; /* * Aceitar somente o domínio canônico. */ if (!in_array($host, ['chcsaude.org', 'www.chcsaude.org'], true)) { return null; } /* * Ignorar páginas que não devem ser indexadas. */ $blockedPaths = [ '/controle', '/busca', '/obrigado', '/test', '/login', '/admin', ]; foreach ($blockedPaths as $blockedPath) { if ( $path === $blockedPath || str_starts_with($path, $blockedPath . '/') ) { return null; } } /* * Remover paginação do sitemap. */ if ($query !== null) { parse_str($query, $queryParameters); if (isset($queryParameters['page'])) { return null; } } /* * Remover PDFs do sitemap principal. * As páginas do Portal da Transparência permanecem. */ if (str_ends_with(strtolower($path), '.pdf')) { return null; } /* * Normalizar a URL: * - sempre HTTPS; * - sem www; * - sem barra final, exceto na home; */ $normalizedPath = $path === '/' ? '/' : '/' . trim($path, '/'); $normalizedUrl = $baseUrl . $normalizedPath; /* * Manter parâmetros úteis, como location_id. */ if ($query !== null) { parse_str($query, $queryParameters); $allowedQueryParameters = []; if (!empty($allowedQueryParameters)) { $normalizedUrl .= '?' . http_build_query( $allowedQueryParameters ); } } $url->url = $normalizedUrl; /* * Configurações coerentes por tipo de página. */ if ($normalizedPath === '/') { return $url ->setPriority(1.0) ->setChangeFrequency(Url::CHANGE_FREQUENCY_WEEKLY); } if (str_starts_with($normalizedPath, '/noticias/')) { return $url ->setPriority(0.8) ->setChangeFrequency(Url::CHANGE_FREQUENCY_WEEKLY); } if ( $normalizedPath === '/portal-da-transparencia' || str_starts_with( $normalizedPath, '/portal-da-transparencia/' ) ) { return $url ->setPriority(0.8) ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY); } if ( $normalizedPath === '/servicos' || str_starts_with($normalizedPath, '/servicos/') ) { return $url ->setPriority(0.8) ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY); } return $url ->setPriority(0.7) ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY); }) ->writeToFile($sitemapPath); return response()->file($sitemapPath, [ 'Content-Type' => 'application/xml; charset=UTF-8', 'Cache-Control' => 'no-cache, no-store, must-revalidate', ]); })->name('front.sitemap'); Server Error
500
Server Error