icamys / php-sitemap-generator

A simple PHP sitemap generator.
MIT License
168 stars 65 forks source link

Please add "base sitemap url" #24

Closed svbackend closed 1 year ago

svbackend commented 3 years ago

Hi, thanks for the library, works great, but I have small issue:

Even though my site base url is "site.com" and every link inside of sitemaps generated correctly but in the sitemap-index.xml links to sitemaps are broken because my "sitemap base url" is "site.com/api/sitemap"

        $siteUrl = 'https://site.com';
        $outputDir = __DIR__ , '/../../var/sitemap';

        $generator = new SitemapGenerator($siteUrl, $outputDir);

        // Add 50000+ urls
        // $generator->addURL('/' . $url['path'], $url['updated_at'], $url['change_freq'], 0.5, $url['alternates']);
        // $generator->flush();

        // Workaround:
        $ref = new \ReflectionObject($generator);
        $baseUrlProperty = $ref->getProperty('baseURL');
        $baseUrlProperty->setAccessible(true);
        $baseUrlProperty->setValue($generator, "{$siteUrl}/api/v1/sitemap"); // Fix is here
        $baseUrlProperty->setAccessible(false);

        $generator->finalize();

It generates exactly what I need:

In index:

 <sitemap>
  <loc>https://site.com/api/v1/sitemap/sitemap1.xml</loc>
  <lastmod>2021-02-02T16:15:58+00:00</lastmod>
 </sitemap>

While in sitemap1.xml

<url>
  <loc>http://site.com/blog</loc>
  ...

So would be nice to have a option to change sitemap base url in case when sitemap located not in the root of website

time85 commented 2 years ago

Hi Dmitrii,

thank you for your effort in that library!

Unfortunately i have the same problem then mentioned above.

I would like to fix it and introduce an additional parameter for it and in case that the parameter is set -> change the path only inside the index-file

I would create & submit a pull request for that issue, if that's fine for you?

icamys commented 1 year ago

Thanks everyone for your contribution. The feature is now available using setSitemapIndexURL() starting from version 5.0.0.