Sitemaps easiest way

With the KISS principle I say: “Keep it simple and straightforward”!

Kirby 2.x:

Installation

I added one line to the code http://getkirby.com/blog/xmlsitemap from @bastianallgeier in line 4, beginning with "$templateignore = ", and one line in line 16, beginning with “<?php if(in_array($p->template(),” and added “->visible()” in the foreach line, to get:

<?php

$ignore = array('sitemap', 'error');                       // pages with uri IN $ignore are ignored
$templateignore = array('internal', 'members' , 'member'); // pages with template IN $templateignore are ignored

// 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()->visible() as $p): ?>
  <?php if(in_array($p->uri(), $ignore)) continue ?>
  <?php if(in_array($p->template(), $templateignore)) continue ?>
  <url>
    <loc><?php echo html($p->url()) ?></loc>
    <lastmod><?php echo $p->modified('c') ?></lastmod>
    <priority><?php echo ($p->isHomePage()) ? 1 : number_format(0.5/$p->depth(), 1) ?></priority>
  </url>
  <?php endforeach ?>
</urlset>

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

Now we can exclude pages and templates.
In my Kirby-websites, all internal (not public) webpages have a special template, which follows the instructions from http://getkirby.com/docs/solutions/authentication to protect the page from viewing by everybody. So we can exclude them very easy.

Hint:

My new lines may be usefull in public menus too.

Set up the routes

With @bastianallgeier at https://github.com/getkirby/kirby/issues/93:

Open your file site/config/config.php and add the following lines, if there are no routes.
If you find there one or some routes, only add the two nested array’s in front of your existing route(s).


/*
---------------------------------------
Setup Kirby Routes
---------------------------------------
*/
c::set('routes', array(
    /* https://github.com/getkirby/kirby/issues/93 */
    array(
        'pattern' => 'sitemap.xml',
        'action'  => function() {
            return site()->visit('sitemap');
        }
    ),
    array(
        'pattern' => 'sitemap',
        'action'  => function() {
            return go('sitemap.xml');
        }
    ),
));

=>

Good luck!

4 Likes