Sitemap with file extension .xml dosen't work

Hello,
try to install sitemap.xml

worked: myhomepage.de/sitemap
do not worked: sitemap.xml

what i do wrong?

template sitemap.php

<?php


$ignore = array('sitemap', 'error');                    
$templateignore = array('team'); 
// 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 ?>
    <?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(1/$p->depth(), 2) ?></priority>
  </url>
  <?php endforeach ?>
</urlset>

config.php

c::set('debug',true);
c::set('cache.ignore', array('sitemap'));

c::set('languages', array(
  array(
    'code'    => 'de',
    'name'    => 'DE',
    'default' => true,
    'locale'  => 'de.utf-8',
    'url'     => '/',
  ),
  array(
    'code'    => 'fr',
    'name'    => 'FR',
    'locale'  => 'fr.utf-8',
    'url'     => '/fr',
  )

));

content file

content/sitemap/xmlsitemap.de.txt

The name of your text file and the name of your template file should be the same, if what you posted above is correct you have:

  • xmlsitemap.de.txt but
  • sitemap.php

instead of xmlsitemap.php

The tutorial has been migrated from Kirby 1. In Kirby 2 between versions 2.0.0 and 2.3.2, the extension is not ignored like in Kirby 1. However the old behavior will work again with the new content representations feature in Kirby 2.4. Until then, you need to name the page content/sitemap.xml for it to work.

@lukasbestle

do not work
if i change content/sitemap/sitemap.de.txt to content/sitemap/sitemap.xml
-> errorPage

is the file extension very important for the search engines crawler ?

@Perry:
Of course you have to 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!

P.S .:
For details e.g. look at
##Sitemaps easiest way

Thank you @HeinerEF

What i don’t understand how do exclude the children of a page ?

Check out this post: Exclude entire subdirectory from sitemap

@Perry:
As I have said:
“For details e.g. look at
##Sitemaps easiest way.”

In that reverenced post I have added two lines which I have reported too within my Hint.

Good luck!

Yes i read the articles and exclude pages with a specific template work
but not children of a page.

What i tried to exclude all childrens from content/ueber-uns/team:

<?php

$ignore = array('sitemap', 'error');                    

$excluded = page('ueber-uns')->children('team');
// 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()->not($exclude) as $p): ?>
  <?php if(in_array($p->uri(), $ignore)) continue ?>


  <url>
    <loc><?php echo html($p->url()) ?></loc>
    <lastmod><?php echo $p->modified('c') ?></lastmod>
    <priority><?php echo ($p->isHomePage()) ? 1 : number_format(1/$p->depth(), 2) ?></priority>
  </url>
  <?php endforeach ?>
</urlset>

Two problems here:

  1. You defined a variable $excluded but you use $exclude in this bit of code

    <?php foreach($pages->index()->not($exclude) as $p): ?>
    

    This would issue a warning if you had debugging turned on.

  2. Children does not take a parameter. So this code won’t work.

    $excluded = page('ueber-uns')->children('team');
    

    The correct way to get the children of team is:

    $excluded = page('ueber-uns')->children()->find('team')->children();
    

It’s not even necessary to use the if statement:

<?php 
$excluded = page('ueber-uns/team')->children();
$ignore = array('sitemap', 'error');  
foreach($pages->index()->not($excluded)->not($ignore) as $p): ?>

@texnixe thank you for helping

$excluded = page('ueber-uns')->children()->find('team')->children();
$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">
// pages without excluded pages
<?php foreach($pages->index()->not($excluded)->not($ignore) as $p): ?>


  <url>
    <loc><?php echo html($p->url()) ?></loc>
    <lastmod><?php echo $p->modified('c') ?></lastmod>
    <priority><?php echo ($p->isHomePage()) ? 1 : number_format(1/$p->depth(), 2) ?></priority>
  </url>
  <?php endforeach ?>
</urlset>

Great that it’s working now. One note regarding:

It would have been content/sitemap.xml/sitemap.de.txt.