Uncategorized

Handling Large Sitemaps in Laravel with Spatie’s Sitemap Package

Sitemaps are vital to your site’s SEO as they guide search engines to the most essential pages of your website. If your website contains a vast amount of data (say, over 50,000 URLs), creating and managing a sitemap can be challenging. This is where Spatie’s Laravel Sitemap package comes in handy. In this article, we’ll explore how to use this package to generate and manage large sitemaps efficiently.

Installation

Firstly, we need to install the package. You can do so via composer:

Problem Statement

If your website has more than 50,000 URLs, generating a sitemap can be a challenge. The Sitemaps protocol limits a sitemap file to 50,000 URLs and 50MB uncompressed. If you need to list more than that, you have to create multiple Sitemap files.

To tackle this, we’ll break our sitemap into smaller parts, each containing no more than 20,000 URLs. For instance, if we have a model called Post with 1 million records, we’ll create a series of sitemap files, each containing 20,000 URLs.

Solution

We’ll create a custom Artisan command that will generate multiple sitemaps and a sitemap index. The index will include links to all of our sitemaps.

Here’s how you can accomplish this:

In this script, the loop starts from 0, and the sitemap files are named sitemap-0.xml, sitemap-1.xml, etc. The sitemap-index.xml file will reference all the individual sitemap files.

Remember to register the new command in your Kernel.php file:

Related Post

To generate the sitemaps, run the following Artisan command:

This will create a new sitemap file for every 20,000 Post records, up to a total of 1 million records, and then it will write an index file that references all the individual sitemap files.

Keeping Sitemaps Updated

You should run this command regularly to keep your sitemaps updated. You can use Laravel’s task scheduling to run the command once a day:

This will ensure that your sitemap remains current with your website’s content.

Wrapping Up

Managing large sitemaps can be a daunting task, but with Laravel and the Spatie Sitemap package, it becomes a breeze. By breaking down the sitemap into smaller chunks, you can efficiently handle a larger number of URLs without violating the sitemap protocol. As a result, your website becomes more accessible and SEO-friendly.

This post was last modified on June 17, 2023 2:28 pm

Dharmesh Goyal

Dharmesh is Co-Founder of TechnoFizi and a passionate blogger. He loves new Gadgets and Tools. He generally covers Tech Tricks, Gadget Reviews etc in his posts. Beside this, He also work as a SEO Analyst at TechnoFizi Solutions.

Share
Published by
Tags: laravel

Recent Posts

Best AI Tools for Summarizing PDFs: The Ones I Use In 2026

Best AI Tools for Summarizing PDFs can help you understand lengthy documents in minutes instead…

1 week ago

Live Betting vs Pre-Match Betting at World Cup 2026

The 2026 World Cup will have more matches than: an amazing 104, all crammed into…

2 weeks ago

5 Best RewritePal Alternatives in 2026 (Free + Paid Picks)

Looking for the best RewritePal alternatives in 2026? Explore the top 5 free and paid…

2 weeks ago

Best 10 Ai Cinematic Video Generator For Free Online Tool In 2026

If you are searching for the best AI Cinematic Video Generator to create viral cinematic…

1 month ago

How Car Crash Victims Can Protect Their Legal Rights Early On

The steps taken immediately after a car crash can have a lasting impact on both…

1 month ago

10 Best AI Apps Every Creator & Professional Needs on Their Phone in 2026 (Free + Paid Picks)

Over the past few days, I have been utilizing a wide array of AI tech…

1 month ago