Discuss your project
Partitech Sonata Extra Bundle

Sonata Extra Bundle: generate a sitemap

Integration with PrestaSitemapBundle Sonata Extra integrates with PrestaSitemapBundle to generate a file sitemap.xml. The generation covers images, articles, and content pages of the application. The process is fully automated and includes support for multi...

Integration with PrestaSitemapBundle

Sonata Extra integrates with PrestaSitemapBundle to generate a file sitemap.xml. The generation covers images, articles, and content pages of the application. The process is fully automated and includes support for multilingual links.

Expand the sitemap

Users wishing to further customize the sitemap, particularly by adding additional resources from Symfony controllers, are encouraged to consult the official documentation of the "PrestaSitemapBundle." This documentation provides detailed instructions and examples on how to effectively use the bundle to extend the sitemap features in Symfony-based applications.

The official documentation specifically allows you to:

  • Understand the main features of the "PrestaSitemapBundle".
  • Learn how to add URLs or custom resources to the sitemap.
  • Implement configurations adapted to more complex sites.

Basic configuration

Let's take a site available in four languages: https://my-website.dev/{en,fr,es,it}. Each locale has its own version of the sitemap.

For a moderate content volume, you can use the on-the-fly generation of PrestaSitemapBundle. Adjust the settings to your project:

# config/packages/presta_sitemap.yaml
presta_sitemap:
  defaults:
    priority: 1
    changefreq: daily
    lastmod: now
# config/routes.yaml
presta_sitemap:
  resource: "@PrestaSitemapBundle/config/routing.yml"
# public/robots.txt
User-agent: *
Disallow: /admin/
Sitemap: https://my-website.dev/en/sitemap.xml
Sitemap: https://my-website.dev/fr/sitemap.xml
Sitemap: https://my-website.dev/es/sitemap.xml
Sitemap: https://my-website.dev/it/sitemap.xml

That's all.

With a large volume of articles, prefer a disk-based planned generation.
Place the generated files in a dedicated public directory: public/sitemaps/Each language will have its respective directory, so:

public/sitemaps/en
public/sitemaps/fr
public/sitemaps/es
public/sitemaps/it
# config/packages/presta_sitemap.yaml
presta_sitemap:
  defaults:
    priority: 1
    changefreq: daily
    lastmod: now

Remove the route if you disable on-the-fly generation:

# config/routes.yaml
#presta_sitemap:
#  resource: "@PrestaSitemapBundle/config/routing.yml"

Log in to your shell and run the generation commands. PrestaSitemapBundle already exposes the generation command; you just need to provide it with the HTTP context and the output directory:

SYMFONY_HTTP_CONTEXT_URL=""https://my-website.dev/ensymfony console presta:sitemaps:dump public/sitemaps/en/ --base-url https://my-website.dev/en

Then decline the command for each locale in a script that can be automated:

#!/bin/sh

SYMFONY_HTTP_CONTEXT_URL="https://my-website.dev/en" symfony console presta:sitemaps:dump public/sitemaps/en/  --base-url https://my-website.dev/sitemaps/en
SYMFONY_HTTP_CONTEXT_URL="https://my-website.dev/fr" symfony console presta:sitemaps:dump public/sitemaps/fr/  --base-url https://my-website.dev/sitemaps/fr
SYMFONY_HTTP_CONTEXT_URL="https://my-website.dev/es" symfony console presta:sitemaps:dump public/sitemaps/es/  --base-url https://my-website.dev/sitemaps/es
SYMFONY_HTTP_CONTEXT_URL="https://my-website.dev/it" symfony console presta:sitemaps:dump public/sitemaps/it/  --base-url https://my-website.dev/sitemaps/it

Run this script with cron or your usual scheduler. Here is the result:

❯ tree public/sitemaps/ -l
public/sitemaps/
├── en
│   ├── sitemap.Articles.xml
│   ├── sitemap.Medias.xml
│   ├── sitemap.Pages.xml
│   └── sitemap.xml
├── es
│   ├── sitemap.Medias.xml
│   ├── sitemap.Pages.xml
│   └── sitemap.xml
├── fr
│   ├── sitemap.Articles.xml
│   ├── sitemap.Medias.xml
│   ├── sitemap.Pages.xml
│   └── sitemap.xml
└── it
    ├── sitemap.Medias.xml
    ├── sitemap.Pages.xml
    └── sitemap.xml

The last thing is to create your robots.txt file to point to your sitemaps.

# public/robots.txt
User-agent: *
Disallow: /admin/
Sitemap: https://my-website.dev/sitemaps/en/sitemap.xml
Sitemap: https://my-website.dev/sitemaps/fr/sitemap.xml
Sitemap: https://my-website.dev/sitemaps/es/sitemap.xml
Sitemap: https://my-website.dev/sitemaps/it/sitemap.xml

Control the generated result

Sonata Extra Sitemap Generation Flow

After generation, check the HTTP code, XML validity, canonical URLs, and links hreflang. The file robots.txt must reference the public URL of the sitemap, without blocking the resources necessary for rendering.

Share this article