Statamic

The Easiest Way to Create a XML Sitemap for Statamic

Photo of Zac VineyardZac Vineyard
November 3rd, 2023

Below are steps outlining the easiest way to create a sitemap for a website using Statamic as a CMS. This works in Statamic versions 3 and 4.

Step 1

Create a new file in your /resources/views folder called "sitemap.antlers.html" and add the following code to the file. This code can loop over page collections or navigation objects to create your XML.

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {{ collection:pages }}
    <url>
        <loc>{{ permalink }}</loc>
        <lastmod>{{ last_modified format="Y-m-d" }}</lastmod>
        <changefreq>weekly</changefreq>
        <priority>{{ if url == homepage }}0.8{{ else }}0.5{{ /if }}</priority>
    </url>
    {{ /collection:pages }}
</urlset>

Step 2

Add a route pointing to your XML content in you /routes/web.php file. This won't be a traditional Laravel Route, but will, instead, use a Route::statamic() method.

Route::statamic('sitemap.xml', 'sitemap', ['layout' => 'sitemap', 'content_type' => 'xml']);

That's It!

You can now see your XML sitemap at /sitemap.xml.

Copyright © Zachary Vineyard