Guide
Sanity Sitemap and Redirects: Keep Rankings When URLs Change
Sanity does not generate a sitemap or handle redirects on its own — both live in your frontend. Build the sitemap from a GROQ query, and store redirects as Sanity documents that your frontend applies as 301s. Together they are what keeps rankings intact when URLs change, including a WordPress to Sanity migration.
Generate a Sanity sitemap in Next.js
Query published documents that have a slug and are not set to noindex, then return them from app/sitemap.ts. GROQ reads published content only unless you pass a token with the drafts perspective, so drafts never leak into the sitemap.
import type { MetadataRoute } from 'next'
import { client } from '@/sanity/client'
const BASE_URL = 'https://example.com'
const query = `*[
_type in ["page", "post"] &&
defined(slug.current) &&
seo.robots.noIndex != true
]{
_type,
"slug": slug.current,
_updatedAt
}`
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const docs = await client.fetch<{ _type: string; slug: string; _updatedAt: string }[]>(query)
return docs.map((doc) => ({
url: doc._type === 'post' ? `${BASE_URL}/blog/${doc.slug}` : `${BASE_URL}/${doc.slug}`,
lastModified: doc._updatedAt,
}))
}Use the same URL logic for the sitemap and the canonical tag, so every sitemap entry is the canonical version of its page. Past roughly 50,000 URLs, split it with generateSitemaps.
Manage redirects in Sanity Studio
A small document type lets editors add redirects without touching code — useful when a slug changes or a page is merged.
import { defineField, defineType } from 'sanity'
export default defineType({
name: 'redirect',
title: 'Redirect',
type: 'document',
fields: [
defineField({
name: 'source',
title: 'From path',
type: 'string',
description: 'Old path, e.g. /2019/05/my-post/',
validation: (rule) => rule.required().regex(/^\//, { name: 'leading slash' }),
}),
defineField({
name: 'destination',
title: 'To path or URL',
type: 'string',
validation: (rule) => rule.required(),
}),
defineField({
name: 'permanent',
title: 'Permanent (301)',
type: 'boolean',
initialValue: true,
}),
],
})Apply the redirects in Next.js
The simplest option loads redirects at build time. Redirects then update on the next deploy — trigger one from a Sanity webhook if editors need them live sooner.
import { createClient } from '@sanity/client'
const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
apiVersion: '2025-01-01',
useCdn: false,
})
/** @type {import('next').NextConfig} */
const nextConfig = {
async redirects() {
return client.fetch(
`*[_type == "redirect" && defined(source) && defined(destination)]{
source, destination, "permanent": coalesce(permanent, true)
}`,
)
},
}
export default nextConfigFor thousands of redirects, or to change them without a deploy, look the path up in middleware instead and cache the map at the edge.
Migration checklist: WordPress to Sanity without losing rankings
- Export every indexable WordPress URL (from its sitemap or a crawl) before you switch DNS.
- Map each old URL to its new Sanity URL. Keep slugs where you can — every unchanged URL is one less redirect.
- Import the map as redirect documents in Sanity, so editors can fix gaps without a deploy.
- Ship the new sitemap at the same /sitemap.xml path and resubmit it in Search Console.
- Watch Search Console's Pages report and 404s for the first four weeks and add redirects for anything missed.
Carry over titles, descriptions and social images too: import them into seoFields so the new pages keep the snippets that were already earning clicks. The WordPress to Sanity SEO migration article covers content mapping in more depth.