php-sitemap-generator icon indicating copy to clipboard operation
php-sitemap-generator copied to clipboard

Sitemap index not created if added urls < `setMaxUrlsPerSitemap`

Open yharahuts opened this issue 2 years ago • 4 comments

Having this code:

$config = new Config();
$config->setBaseURL( $host );
$config->setSaveDirectory( static::SITEMAP_DIR );

$generator = new SitemapGenerator($config);
$generator->setMaxUrlsPerSitemap( 50000 );
$generator->setSitemapIndexFileName( 'sitemap.xml' );
$generator->setSitemapFileName( 'sitemap-item-.xml' );

foreach( range( 0, 9999 ) as $i ) {
	$generator->addURL( "/url/{$i}" );
}

$generator->flush();
$generator->finalize();

The generator will only create a single file named sitemap-item-.xml.

This is unexpected (at least, for me?), and I'd want it to create a sitemap-item-1.xml and sitemap.xml as sitemap index.

It works as I want if I change MaxUrlsPerSitemap to be lower than urls count:

// will create three files: sitemap.xml as sitemap index, sitemap-item-1.xml and sitemap-item-2.xml as sitemaps.
$generator->setMaxUrlsPerSitemap( 9999 );

Is there any setting I've missed or this is a bug?

yharahuts avatar Nov 03 '23 11:11 yharahuts

@yharahuts Well, you don't need an index file if all fits in one sitemap. But one problem i still have, it wont delete old sitemaps when generating less sitemap-{x}.xml files than before. And it does not add the index to the robots file

darvinde avatar Nov 13 '23 13:11 darvinde

@darvinde I agree, but then this single file must be named same as sitemap index file. Currently it's named sitemap-item-.xml which is kinda broken.

yharahuts avatar Nov 13 '23 13:11 yharahuts

@yharahuts Hmmm, why is it broken? You wrote $generator->setSitemapFileName( 'sitemap-item-.xml' ); so you better should change that line. sitemap.xml is the default name for most search engines if no index file exist.

darvinde avatar Nov 13 '23 13:11 darvinde

@darvinde I'm expecting to get entry point file (either sitemap index or single sitemap file) to be same name (e.g. sitemap.xml) regardless of added urls count. But for now, I'm getting two distinct options which I could not control (or could I?).

So for me best options could be:

  • always create index (sitemap.xml) and lists;
  • or rename sitemap to sitemap.xml (as set in setSitemapIndexFileName) if there's no sitemap index because of low urls count.

yharahuts avatar Nov 13 '23 14:11 yharahuts