Sitemaps easiest way

Hello everyone,
Let me present to you the easiest way to have sitemaps installed on your server. Its just a matter of copy and paste. To install; just download and paste the sitemap.php file into the directory where you’ve installed kirby. Open the url of the sitemap.php in a browser.That’s it. This code is also open for recommendations. If you found an error pls dont hesitate to tell me. Thanks, and im happy to help you. Please take note that sitemap.php (without “s”) must be the name of the php file otherwise you’ll encounter problems, However, if you really wanted to rename it like xmlsitemap.php you should also edit the sitemap.php in line 16.
A sincere message from the Philippines. :wink:

1 Like

What are you using the regular expressions for? Is there an advantage over the version at http://getkirby.com/blog/xmlsitemap?

2 Likes

I include a “sitemap” field in all my blueprints and filter the xmlsitemap results that way. If you use a one page setup, or if you have pages that have children that act as content instead of another page (which I imagine nearly every kirby installation does), you’ll want more control over it.

For instance, if I had a slideshow on the home page, it’s content/home/slideshow/slide-1 , slide-2, slide-3. I don’t want to include the slide-1,2,3 directories… or even slideshow directory they reside in for that matter because they should never be accessed directly.

3 Likes

I think that’s actually the best way of doing it because otherwise you end up adding not() methods to your sitemap whenever you add new content or risk having stuff in your sitemap that you don’t want there at all …

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

Actually this method is the easiest method to install sitemap in kirby and that is the main advantage. Though I forget to put the most important line, the line that can allow you to exclude pages in the sitemap (I am really sorry for that, I’ll fix as soon as possible). Another thing, there are some replies I don’t understand, because im not that expert with kirby, Im just 14 and i still need to study.You are free to experiment with the codes and send me a pull request on github. Thank you. :slight_smile:

The functions are used to filter the url by removing the file base name in the <loc></loc> tag. Without those functions you will end up having a url in the loc tag like this:
http://localhost/sitemap.php/parentpage/childpage/grandchildpage/
instead of this:
http://localhost/parentpage/childpage/grandchildpage/
:smile:

An old thread now but I just tested it and I like how it looks for most part.

Why sitemap as content page?

However I don’t like to create a content page just for it to show up on the site. We are not adding any content to it in the panel (in this example).

Route it

I solved it, almost “by the book” but with one different thing. I load a template directly from the route. Then I don’t need to create content I don’t need.

c::set('routes', array(
	array(
		'pattern' => 'sitemap.xml',
		'action'  => function() {
			tpl::load(kirby()->roots()->templates() . DS . 'sitemap.php', array(), false );
		}
	),
	array(
		'pattern' => 'sitemap',
		'action'  => function() {
			return go('sitemap.xml');
		}
	),
));

Minor fix to the template

I use this template for it:

http://getkirby.com/blog/xmlsitemap

A minor problem is that $page is unknown because the template is called directly from the route. It’s easily fixed by adding this row on line 2.

$pages = site()->pages();
1 Like

An even shorter version

Put this in your templates/sitemap.php like before:
http://getkirby.com/blog/xmlsitemap

The route

c::set('routes', array(
	array(
		'pattern' => 'sitemap.xml',
		'action'  => function() {
			tpl::load(kirby()->roots()->templates() . DS . 'sitemap.php', array( 'pages' => site()->pages() ), false );
		}
	),
));

That’s it.

  • Now you don’t have to modify the template in any way.
  • There is no need to redirect /sitemap because we don’t use the content method.

As a plugin

In case you want it as a plugin, just save the above code in /site/plugins/sitemap/sitemap.php and change the first row to this:

kirby()->routes( array(
5 Likes

Thanks for sharing. This was very helpful!

[Edit: I was looking for a way to load a template file for a custom route, and this did the trick]

HI,
there is also a plugin listet in the Plugins & Extensions Site

1 Like

hey guys i have tried all the above. In my installation it works for the route /sitemap but does not work for the .xml extension. I tried to redirect the page but it does not even seem to get this far. My redirects are working for normal page but not as soon as i add an extension to the url. Any ideas what the reason could be? Maybe its something inside the default .htaccess ?

EDIT: Found out that this problem only exists when working locally on a server it works fine. So ensure to try it online before wasting your time. Although it would be interesting to know the reason.

@nerdmed:
Hint:
I don’t know, but I think, you don’t have ONLY one line starting with

in your file site/config/config.php. You need only ONE line of this, not none or more!

And that would be your problem I think.

Good luck!

hey @anon77445132 thanks for your reply. Actually I am aware that there only can be one routes array. It still works on my production server but not on localhost.

@nerdmed:
Look whether you have more than one config*.php file on your localhost webserver like config.127.0.0.1.php or config.localhost.php.

For details look at Docs: Multi-environment setup.

What is your current setup?