Sitemap for search engines
For basic search engine optimization it's good to have a sitemap.xml file, which contains machine-readable information about all the pages and the structure of your site.
The syntax for such a sitmap.xml file is fairly simple. You can find all about it on http://www.sitemaps.org/
Because you probably don't want to generate that file by hand, I've created a little template for you, which automatically indexes your entire site and generates a sitemap.xml for you:
<?php
$ignore = array('sitemap', 'error');
// send the right header
header('Content-type: text/xml; charset="utf-8"');
// echo the doctype
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach($pages->index() as $p): ?>
<?php if(in_array($p->uri(), $ignore)) continue ?>
<url>
<loc><?= html($p->url()) ?></loc>
<lastmod><?= $p->modified('c') ?></lastmod>
<priority><?= ($p->isHomePage()) ? 1 : number_format(0.5/$p->depth(), 1) ?></priority>
</url>
<?php endforeach ?>
</urlset>
Installation
Copy the entire code from above and put it in your site/templates
directory in a new file called xmlsitemap.php
Go to your content folder and create a new invisible folder called sitemap
and also add a text file to it called xmlsitemap.txt
(and don't forget to add the language extensions if you are on a multi-language site).
You should now be able to visit the sitemap of your Kirby site by browsing http://yourdomain.com/sitemap
or http://yourdomain.com/sitemap.xml
(Kirby ignores the extension, so you can use both)
Exclude pages
To exclude certain subpages of your site, you can add their URI to the $ignore
array. I've already done that for the sitemap page itself and also for the error page.
$ignore = array('sitemap', 'error');
Caching
If you are using the HTML cache, you should make sure to avoid caching of the sitemap file. Go to site/config/config.php
and change the cache settings like this:
c::set('cache.ignore', array('sitemap'));
This will make sure that your sitemap file is always served as proper XML.
Automatic Priorities
The sitemap template will automatically try to create priorites for each page, by using the ->depth()
attribute. The depth attribute is a simple number, which indicates how deep the page is nested. Main pages have a depth of 1, subpages of main pages have a depth of 2, etc.
The home page will automatically get a priority of 1. You can easily change priorities or remove them entirely if you prefer.
Submitting your sitemap to a search engine
To let your favorite search engines know about your brand new sitemap, please read the instructions on sitemap.org: http://www.sitemaps.org/protocol.html#informing
Improve it!
I'm always looking for contributers, to improve Kirby's extensions. I'm not a specialist with Search Engine Optimization, so if you have ideas how to improve the xmlsitemap.php template or any other extension, just let me know!!