Splitting large sitemaps | laravel-sitemap | Spatie

 SPATIE

  Laravel Sitemap
==================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-sitemap](https://spatie.be/docs/laravel-sitemap/v8)  Creating-sitemaps  Splitting large sitemaps

 Version   v8

 Other versions for crawler [v8](https://spatie.be/docs/laravel-sitemap/v8)

- [ Introduction ](https://spatie.be/docs/laravel-sitemap/v8/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-sitemap/v8/installation-setup)
- [ Support us ](https://spatie.be/docs/laravel-sitemap/v8/support-us)
- [ Changelog ](https://spatie.be/docs/laravel-sitemap/v8/changelog)
- [ Questions and issues ](https://spatie.be/docs/laravel-sitemap/v8/questions-issues)
- [ About us ](https://spatie.be/docs/laravel-sitemap/v8/about-us)

Creating sitemaps
-----------------

- [ Generating a sitemap ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/generating-a-sitemap)
- [ Manually creating a sitemap ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/manually-creating-a-sitemap)
- [ Creating a sitemap index ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/creating-a-sitemap-index)
- [ Splitting large sitemaps ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/splitting-large-sitemaps)
- [ Adding an XSL stylesheet ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/adding-an-xsl-stylesheet)
- [ Returning a response ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/returning-a-response)
- [ Sorting URLs ](https://spatie.be/docs/laravel-sitemap/v8/creating-sitemaps/sorting-urls)

Customizing the crawler
-----------------------

- [ Crawl profiles ](https://spatie.be/docs/laravel-sitemap/v8/customizing-the-crawler/crawl-profiles)
- [ Filtering URLs ](https://spatie.be/docs/laravel-sitemap/v8/customizing-the-crawler/filtering-urls)
- [ Configuring the crawler ](https://spatie.be/docs/laravel-sitemap/v8/customizing-the-crawler/configuring-the-crawler)

Adding content to URLs
----------------------

- [ Alternates ](https://spatie.be/docs/laravel-sitemap/v8/adding-content-to-urls/alternates)
- [ Images ](https://spatie.be/docs/laravel-sitemap/v8/adding-content-to-urls/images)
- [ Videos ](https://spatie.be/docs/laravel-sitemap/v8/adding-content-to-urls/videos)
- [ News ](https://spatie.be/docs/laravel-sitemap/v8/adding-content-to-urls/news)
- [ Sitemapable models ](https://spatie.be/docs/laravel-sitemap/v8/adding-content-to-urls/sitemapable-models)

Advanced usage
--------------

- [ Scheduled generation ](https://spatie.be/docs/laravel-sitemap/v8/advanced-usage/scheduled-generation)
- [ Writing to disks ](https://spatie.be/docs/laravel-sitemap/v8/advanced-usage/writing-to-disks)

 Splitting large sitemaps
========================

###  On this page

1. [ When using the sitemap generator ](#content-when-using-the-sitemap-generator)
2. [ When building a sitemap manually ](#content-when-building-a-sitemap-manually)

When you have a large number of URLs, you can automatically split them into multiple sitemap files with a sitemap index. Call the `maxTagsPerSitemap` method to set the maximum number of URLs per file.

When using the sitemap generator
--------------------------------------------------------------------------------------------------------------------------------------------------------

```
use Spatie\Sitemap\SitemapGenerator;

SitemapGenerator::create('https://example.com')
    ->maxTagsPerSitemap(20000)
    ->writeToFile(public_path('sitemap.xml'));
```

When building a sitemap manually
--------------------------------------------------------------------------------------------------------------------------------------------------------

This also works when building a sitemap manually. When the number of URLs exceeds the limit, `writeToFile` and `writeToDisk` will automatically create chunk files (`sitemap_0.xml`, `sitemap_1.xml`, ...) and write a sitemap index as the main file:

```
use Spatie\Sitemap\Sitemap;

Sitemap::create()
    ->maxTagsPerSitemap(20000)
    ->add(/* ... */)
    ->writeToFile(public_path('sitemap.xml'));
```

If the number of URLs is within the limit, a single sitemap file will be written as normal.
