Sub topic 14
Generating sitemaps from your site's data
This chapter assumes you know the sitemap XML structure (urlset, url, loc, lastmod), the 50,000-URL and 50 MB limits, and basic Python. It shows how to build sitemaps from the data your site already holds, instead of editing XML by hand.
Why generate instead of write
A hand-edited sitemap goes stale. New pages are missing, deleted pages stay listed, and lastmod values drift. If your site stores its pages in a database, a CMS, or a folder of files, that source is the right place to build the sitemap from. The generator runs on every deploy or on a schedule, so the sitemap always matches the site.
Step 1: Filter what goes in
Only list URLs you want indexed. Leave out:
- URLs that redirect or return an error status.
- Pages with a
noindexdirective. - URLs blocked by
robots.txt. - Duplicate URLs, such as parameter variants of a canonical page.
Filtering in the generator keeps the sitemap consistent with your other signals.
Step 2: Use a real `lastmod`
Take lastmod from the date the page content last changed, such as an updated_at column. Do not use the time the generator ran. If every URL shows today's date, crawlers learn to ignore the field.
Step 3: Write the files with a XML library
Build the XML with a library, not string concatenation. A URL like https://www.example.com/search?q=a&b=2 contains an &, which must be escaped as &. A library does this for you.
The script below splits pages into chunks and writes an index. It uses a small chunk size so you can test it. In production, set the limit to 50,000.
import xml.etree.ElementTree as ET
NS = "http://www.sitemaps.org/schemas/sitemap/0.9"
ET.register_namespace("", NS)
BASE = "https://www.example.com"
MAX_URLS = 2 # use 50000 in production
def write_sitemap(pages, path):
urlset = ET.Element(f"{{{NS}}}urlset")
for loc, lastmod in pages:
url = ET.SubElement(urlset, f"{{{NS}}}url")
ET.SubElement(url, f"{{{NS}}}loc").text = loc
ET.SubElement(url, f"{{{NS}}}lastmod").text = lastmod
ET.ElementTree(urlset).write(path, encoding="utf-8", xml_declaration=True)
def write_index(names, path, lastmod):
index = ET.Element(f"{{{NS}}}sitemapindex")
for name in names:
sm = ET.SubElement(index, f"{{{NS}}}sitemap")
ET.SubElement(sm, f"{{{NS}}}loc").text = f"{BASE}/{name}"
ET.SubElement(sm, f"{{{NS}}}lastmod").text = lastmod
ET.ElementTree(index).write(path, encoding="utf-8", xml_declaration=True)
def build_all(pages):
names = []
for i in range(0, len(pages), MAX_URLS):
name = f"sitemap-{i // MAX_URLS + 1}.xml"
write_sitemap(pages[i : i + MAX_URLS], name)
names.append(name)
newest = max(lastmod for _, lastmod in pages)
write_index(names, "sitemap_index.xml", newest)
pages = [
(f"{BASE}/", "2026-11-01"),
(f"{BASE}/about", "2026-09-12"),
(f"{BASE}/blog/first-post", "2026-10-15"),
(f"{BASE}/search?q=a&b=2", "2026-11-20"),
(f"{BASE}/contact", "2026-08-30"),
]
build_all(pages)Running this writes sitemap-1.xml, sitemap-2.xml, sitemap-3.xml, and sitemap_index.xml. Open the second file and you will see the & written as &.
Step 4: Run it automatically
Run the generator in your build or deploy pipeline, or on a schedule if content changes without a deploy. Write the files to the site root, then confirm that robots.txt has a Sitemap: line pointing at the index.
Before publishing, check that each file is under both limits. Compressed .xml.gz files still count against 50 MB when uncompressed.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `lastmod` set to the build time | Crawlers stop trusting it | Use the content's real modified date |
| Relative URLs in `loc` | Entries are rejected | Use absolute URLs with scheme and host |
| Unescaped `&` from string joins | The XML fails to parse | Use an XML library |
| Listing redirected or `noindex` URLs | Conflicting signals | Filter them out in the generator |
Exercise
Your generator sets every lastmod to the current date on each run. Why is this a problem, and where should the value come from instead?
Show answer
Crawlers compare lastmod with what they saw before. If every page always looks newly changed, the field carries no information and crawlers learn to ignore it. Take the value from the page's real last-modified date, such as an updated_at column or the content file's modification time.