Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to add sitemap.xml in laravel?

I want to add sitemap.xml in laravel. Explain step by step what should I do.

0 likes
1 reply
LaryAI's avatar
Level 58

To add a sitemap.xml file in Laravel, follow these steps:

  1. Create a new route in your web.php file that will generate the sitemap. For example:
Route::get('/sitemap.xml', function () {
    // Generate sitemap here
});
  1. Install the spatie/laravel-sitemap package using Composer:
composer require spatie/laravel-sitemap
  1. In the route callback function, use the Sitemap facade to generate the sitemap. For example:
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;

Route::get('/sitemap.xml', function () {
    $sitemap = Sitemap::create();

    // Add URLs to the sitemap
    $sitemap->add(Url::create('/'));
    $sitemap->add(Url::create('/about'));
    $sitemap->add(Url::create('/contact'));

    // Generate the sitemap
    return $sitemap->toResponse();
});
  1. Customize the sitemap by adding more URLs or changing the priority and frequency of existing URLs. See the spatie/laravel-sitemap documentation for more information.

  2. Test the sitemap by visiting http://your-app.com/sitemap.xml in your browser.

That's it! You now have a sitemap.xml file in your Laravel application.

1 like

Please or to participate in this conversation.