---
title: "Set canonicals and sitemap with Astro integrations"
description: "Astro needs a production site value before canonicals and sitemaps can be correct. Set site in astro.config.mjs, install the @astrojs/sitemap integration, and let it emit sitemap-index.xml with child files. Then render an absolute canonical in the layout from Astro.url and Astro.site so every page references itself."
url: "https://seo-mcp.io/t/set-canonicals-and-sitemap-with-astro-integrations"
---

# Set canonicals and sitemap with Astro integrations

Astro needs a production site value before canonicals and sitemaps can be correct. Set site in astro.config.mjs, install the @astrojs/sitemap integration, and let it emit sitemap-index.xml with child files. Then render an absolute canonical in the layout from Astro.url and Astro.site so every page references itself.

## Prerequisites

- An Astro project with a production domain
- Ability to run a production build locally or in CI

## Steps

1. **Set the production site** — In astro.config.mjs set site: 'your production domain' using the production origin with no trailing slash. This value feeds canonicals, RSS links and sitemap entries.
   Check: grep -n 'site:' astro.config.mjs shows the production origin and astro build finishes without a site warning.
2. **Install the sitemap integration** — Run npx astro add sitemap, which installs @astrojs/sitemap and adds it to integrations. Build and confirm sitemap-index.xml and its children land in dist/.
   Check: dist/sitemap-index.xml exists after astro build and contains only absolute https URLs.
3. **Render a canonical in the layout** — In src/layouts/Base.astro compute const canonical = new URL(Astro.url.pathname, Astro.site) and add link rel=canonical href={canonical} in the head so each route declares itself.
   Check: curl -s your production domain/ | grep -c 'rel="canonical"' returns 1 with an absolute href.
4. **Filter drafts and previews** — Use the filter option of the sitemap integration to drop draft, preview and thin archive routes, while keeping paginated pages that are meant to be indexable.
   Check: The built sitemap contains no preview or draft URLs and every loc returns 200.
5. **Deploy and submit** — Deploy the build, request sitemap-index.xml in production and add it in Search Console. Confirm robots.txt links to the same index rather than an old sitemap file.
   Check: curl -s your production domain/sitemap-index.xml returns 200 and Search Console reports the sitemap as Success.

## Source

@seo-mcp on 2026-10-19: https://docs.astro.build/en/guides/integrations-guide/sitemap/

Canonical: https://seo-mcp.io/t/set-canonicals-and-sitemap-with-astro-integrations
