To exclude pages from the Astro sitemap, pass a filter function to @astrojs/sitemap. It receives each page’s full URL and keeps the page only when the function returns true, so filter: (page) => !page.includes('/thank-you/') removes the thank-you page. For rules that depend on content, such as a noindex frontmatter field, read the flags in the config file and check them inside filter.
Key Takeaways
filter(page)gets the absolute URL as a string; returnfalseto exclude it.- Status pages such as
404and500are left out automatically.- Keep a single list of excluded paths and use it for both noindex and the sitemap.
serializecan also drop a URL by returningundefined.
Filter by path
// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
const EXCLUDE = ['/thank-you/', '/search/', '/drafts/'];
export default defineConfig({
site: 'https://www.example.com',
integrations: [
sitemap({
filter: (page) => !EXCLUDE.some((path) => new URL(page).pathname.startsWith(path)),
}),
],
});
Comparing new URL(page).pathname is safer than page.includes(), which would also match /blog/search/ when you meant only /search/. startsWith excludes a whole section, such as every page under /drafts/.
Filter by pattern
Regular expressions handle families of URLs, such as paginated archives or tag pages:
// astro.config.mjs (sitemap excerpt)
sitemap({
filter: (page) => {
const { pathname } = new URL(page);
if (/\/page\/\d+\/$/.test(pathname)) return false; // /blog/page/2/
if (pathname.startsWith('/tags/')) return false;
return true;
},
});
Whether to exclude pagination and tag pages is a judgment call. If they have unique, useful content and are indexable, keep them. If they are noindexed, take them out.
Exclude pages marked noindex in frontmatter
The config file cannot import astro:content, but it can read the Markdown files directly. This reads every post’s frontmatter and collects the ones marked noindex: true:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
import { readdirSync, readFileSync } from 'node:fs';
import matter from 'gray-matter';
const dir = './src/content/blog';
const noindexed = new Set(
readdirSync(dir)
.filter((f) => /\.mdx?$/.test(f))
.filter((f) => matter(readFileSync(`${dir}/${f}`, 'utf8')).data.noindex === true)
.map((f) => `/blog/${f.replace(/\.mdx?$/, '')}/`)
);
export default defineConfig({
site: 'https://www.example.com',
integrations: [
sitemap({
filter: (page) => !noindexed.has(new URL(page).pathname),
}),
],
});
Install gray-matter as a dev dependency first. This keeps the noindex setting and the sitemap in sync from one field.
Dropping URLs in serialize
serialize runs on every sitemap entry and can change or remove it. Returning undefined drops the entry, which is handy when you are already using serialize to set lastmod:
// astro.config.mjs (sitemap excerpt)
sitemap({
serialize(item) {
if (item.url.endsWith('/print/')) return undefined;
return item;
},
});
See the lastmod tip for the rest of what serialize can do.
What is already excluded
- Status pages.
404and500pages are filtered out by the integration. - Dynamic on-demand routes. A route such as
src/pages/products/[id].astrowithprerender = falsehas no known URLs at build time, so none are listed; add the ones you want withcustomPages. On-demand routes with a fixed path, such as/search/, are listed, so filter them if needed.
After a build, open dist/sitemap-0.xml and search for a URL you excluded to confirm it is gone. The sitemap setup tip covers installation, and the config docs show where the integration is registered in a theme from our catalog.
FAQ
What does the filter function receive in @astrojs/sitemap?
The full absolute URL of each page as a string, for example https://www.example.com/about/. Parse it with new URL() to compare paths reliably.
Does the Astro sitemap include the 404 page?
No. The integration skips status code pages such as 404 and 500 automatically.
Should noindex pages be in the sitemap?
No. A sitemap signals that a URL should be indexed, so listing noindexed pages sends conflicting signals. Exclude them with filter.
Can I exclude a whole folder from the Astro sitemap?
Yes. Check new URL(page).pathname.startsWith('/folder/') in filter and return false for matches.