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

Grok AI Image Generator – How to Use Grok AI, Image Prompt

Grok AI Image Generator 2025: Do you also want to create an image like the…

2 weeks ago

25+ Best Free AI Maker Tools In 2025

Are you looking for the greatest free AI Maker tools to grow your business? This…

1 month ago

10 Ai Kiss & Hug Video Generator For Free Online Tool In 2025

Hello friends, are you also searching for a tool or website to make AI kissing…

1 month ago

Trending 5 AI Clothes Changer Online Free Tool In 2025

Do you love experimenting with different outfits, AI Clothes Changer or find buying new clothes…

1 month ago

A World of Luxury: Exploring the Perks of Premium Credit Cards

Premium credit cards offer many special benefits to the user. These can enhance a person’s…

2 months ago

A Buyer’s Guide to 45ft Standard Shipping Containers in the United States

Shipping containers are among the most versatile and durable storage solutions available, commonly used for…

3 months ago