Easier sitemap.xml

I’ve been searching for ways to build a sitemap for Kirby and found serveral solutions. The solutions I found didn’t really satisfy me because they all require a template and content folder which makes the sitemap show up in the panel. After playing around I came up with the following solution:

1 - Create a new sitemap.php snippet with the following contents:

<?= '<?xml version="1.0" encoding="utf-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<?php foreach ($pages as $p): ?>
		<?php if (in_array($p->uri(), $ignore) || $p->isInvisible()) continue ?>
			<loc><?= html($p->url()) ?></loc>
			<lastmod><?= $p->modified('c') ?></lastmod>
			<priority><?= ($p->isHomePage()) ? 1 : number_format(0.5 / $p->depth(), 1) ?></priority>
	<?php endforeach ?>

2 - Add the following route to the config.php file:

c::set('routes', array(
		'pattern' => 'sitemap.xml',
		'action'  => function() {
			$pages = site()->pages()->index();
			$ignore = array('error');

			header::contentType('text/xml', 'UTF-8', true);

			snippet('sitemap', compact('pages', 'ignore'));
		'pattern' => 'sitemap',
		'action'  => function() {
			return go('sitemap.xml');

I thought I share it with you, hope you guys like it!

1 Like

This is similar:

Ah, I must have missed that solution!

I removed this post - and created a new topic, may be it can help others more easily when it is placed in it’s own topic?