Skip to content

Adjust router so it works with HEAD request #7

@Nazahim

Description

@Nazahim

It seems bots like Google are having problems requesting the sitemap because they first issue a HEAD request to check for sitemap existence. This HEAD will fail with a 404. I have adjusted the router so it not only catches the GET (default), but also the HEAD as following. This way the HEAD now returns a 200 OK.

(added: ,
'method' => 'GET|HEAD'
--- mind the comma)

'routes' => [
[
'pattern' => 'sitemap.xml',
'action' => function(){
// get function that will be used to generate sitemap::
$mapper = option('kirbyzone.sitemapper.customMap');
// generate sitemap:
$map = $mapper(site());
$hasExtraPages = option('kirbyzone.sitemapper.extraPages');
if(is_callable($hasExtraPages)){
$extra = $hasExtraPages(site());
$map = array_merge($map, $extra);
}
//build the xml document:
$content = snippet('sitemapper/xml', ['map' => $map], true);
// return response with correct header type
return new Kirby\Cms\Response($content, 'application/xml');
},
'method' => 'GET|HEAD'
],

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions