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

nhayder's avatar
Level 13

spatie/laravel-sitemap creating empty sitemap.xml

i'm using spatie/laravel-sitemap to generate sitemap.xml file dynamically, The package is not creating the site map but its always empty.

packager usrl : https://github.com/spatie/laravel-sitemap

created sitemap.xml

// this is what im getting every time
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
</urlset>

this is how im generating the file???

SitemapGenerator::create(config('app.url'))->writeToFile(public_path('sitemap.txt'));

if there is anyone who is using this package? can you see what is the problem

All the best

0 likes
7 replies
bobbybouwmann's avatar

Are you sure the app.url is set to the correct url? This needs to match with the domain of your site.

Also have you tried generating the sitemap yourself using the other calls? Just to try if it works or not!

1 like
Snapey's avatar

Are you running this from a console command? I'm wondering who's account the code runs under

Does your webserver have rights to create the file in the public folder (its not recommended to be writable in production)

If you delete the file, is a new one created?

What laravel version are you on?

Are all your routes in web.php?

1 like
nhayder's avatar
Level 13

@bobbybouwmann actually the app.url was not matching so i changed is like this

Route::get('/sitemap', function(){

    SitemapGenerator::create('http://127.0.0.1:8000')->writeToFile('sitemap.xml');

    return 'Sitemap generated';

});

After the that change the file now need almost 2 minutes for be created and its still empty?

// this is the content of the file after its being created
<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
</urlset>

@snapey to answer you questions, 1- no i'm not using a command the file creation is directly on the web.php file 2- i'm running the app on localhost, but the files is being created every time 3- i'm using laravel 5.8 4- yes all my routes are in web.php

Cronix's avatar
Cronix
Best Answer
Level 67

I haven't used that package, but from the docs, it says it generates the links by actually crawling your site. Perhaps it's needing an external url accessible from the public net, and not a local 127.0.0.1 loopback? Not sure.

2 likes
bobbybouwmann's avatar

@cronix I believe it's crawling it locally as well if you point it to localhost!

@nhySo to comeback to my previous reply. Have you tried manually adding the route to the sitemap generator to check if it can generate at least one route in the xml?

1 like
nhayder's avatar
Level 13

@cronix Yah, :-) worked after i uploaded the site to the internet, Thanx

Samson's avatar

This is I solved this problem.

I just made sure APP_URL in .env file was set properly. I added, for example, https://example.com/, and boom it worked

Please or to participate in this conversation.