> For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt.

# Sitemap.xml: Architecture & Implementation Guide

A `sitemap.xml` file is an XML document that lists the canonical URLs on your site that you want search engines to discover and index. While a sitemap does not guarantee that every listed URL will be crawled or ranked, it acts as a direct discovery feed—especially valuable for large sites, newly published content, or pages buried several clicks deep.

`sitemap.xml` and [`robots.txt`](https://nhanbe.id.vn/building-for-the-web/web-seo/robots-txt.md) play complementary roles:

- **`sitemap.xml` answers:** _"Which canonical URLs exist and should be indexed?"_
- **`robots.txt` answers:** _"Which URL paths should crawlers avoid fetching?"_

## Minimal Valid Structure

A standard XML sitemap must declare the Sitemaps 0.9 XML namespace on a root `<urlset>` element containing one or more `<url>` entries. Each `<url>` requires a `<loc>` child element:

```xml title="sitemap.xml"
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://hotel.example/</loc>
  </url>
  <url>
    <loc>https://hotel.example/rooms/deluxe</loc>
  </url>
  <url>
    <loc>https://hotel.example/locations/da-nang</loc>
  </url>
</urlset>
```

Every `<loc>` value must be a fully qualified **absolute URL** that matches your site's exact canonical protocol (`https://`), hostname (`www` vs. non-`www`), and trailing-slash convention.

## Which URLs Belong in `sitemap.xml`?

Treat your sitemap as a **curated list of canonical search landing pages**, not a dump of every route your router can resolve.

### Include Only URLs That:

- Return **`HTTP 200 OK`**
- Are the **canonical URL** for that content (see the [Canonical URL Guide](https://nhanbe.id.vn/building-for-the-web/web-seo/canonical-url-seo.md))
- Are publicly accessible without login or authentication
- Contain substantive, index-worthy content
- Are **permitted** to be crawled by [`robots.txt`](https://nhanbe.id.vn/building-for-the-web/web-seo/robots-txt.md) and carry no `noindex` directive

### Never Include:

- `3xx` redirecting URLs (`301`, `302`, `307`, `308`)
- `4xx` or `5xx` error pages
- Pages carrying `<meta name="robots" content="noindex">` or `X-Robots-Tag: noindex`
- Non-canonical parameter variants (`?utm_source=...`, `?sort=price`, print views)
- Session-bound, checkout, confirmation, user account, or admin routes
- Internal search result URLs (`/search?q=...`)

:::tip\[Why Sitemap Hygiene Matters in GSC]
Google Search Console's Page Indexing report offers an **All submitted pages** filter based strictly on your sitemaps. Keeping non-canonical and redirecting URLs out of `sitemap.xml` ensures that any `Not indexed` warning in **All submitted pages** immediately highlights a real indexing issue. See the [GSC Page Indexing Playbook](https://nhanbe.id.vn/building-for-the-web/web-seo/google-search-console-page-indexing.md) for details.
:::

## Core XML Sitemap Elements

### `<loc>` (Required)

Contains the full, XML-escaped canonical URL of the page (`&` must be escaped as `&amp;` if query parameters are unavoidable):

```xml
<url>
  <loc>https://hotel.example/rooms/deluxe</loc>
</url>
```

### `<lastmod>` (Recommended)

Indicates the date (or ISO 8601 timestamp) when the page's **meaningful content last changed**:

```xml
<url>
  <loc>https://hotel.example/offers/summer</loc>
  <lastmod>2026-09-20</lastmod>
</url>
```

:::warning\[Do Not Set `<lastmod>` to the Build Timestamp]
Only update `<lastmod>` when the page's primary content or structured data actually changes. Resetting `<lastmod>` to today's date across every URL on every CI/CD deployment destroys signal trust and causes search engines to ignore your `<lastmod>` timestamps altogether.
:::

### `<changefreq>` and `<priority>` (Deprecated in Practice)

Although the Sitemaps 0.9 specification defines `<changefreq>` and `<priority>`, Google and other modern search engines **officially ignore both attributes** because site owners overwhelmingly misconfigured them. Omit them to keep your XML payload small and clean, and focus on accurate `<loc>` and `<lastmod>` values instead.

## Size Limits & Sitemap Index Files

A single XML sitemap file is strictly capped by the protocol at:

- **50,000 URLs** maximum per file
- **50 MB (uncompressed)** maximum file size

When a site grows beyond these limits—or when you want clean observability per content type in Google Search Console—split your URLs across multiple child sitemaps and reference them from a **Sitemap Index** (`<sitemapindex>`):

```xml title="sitemap-index.xml"
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://hotel.example/sitemaps/rooms.xml</loc>
    <lastmod>2026-09-20</lastmod>
  </sitemap>
  <sitemap>
    <loc>https://hotel.example/sitemaps/offers.xml</loc>
    <lastmod>2026-09-18</lastmod>
  </sitemap>
  <sitemap>
    <loc>https://hotel.example/sitemaps/locations.xml</loc>
    <lastmod>2026-09-15</lastmod>
  </sitemap>
</sitemapindex>
```

Partitioning child sitemaps by entity type (`rooms.xml`, `offers.xml`, `blog.xml`) or date range makes it effortless to isolate which section of your site has an indexing drop in Google Search Console. You can also serve gzip-compressed sitemaps (e.g., `rooms.xml.gz`), provided the uncompressed XML still stays under 50 MB and 50,000 URLs.

## Exposing Your Sitemap to Crawlers

### 1. Reference in `robots.txt`

Add a top-level `Sitemap:` directive containing the absolute URL of your sitemap or sitemap index at the bottom of `/robots.txt`:

```txt title="robots.txt"
User-agent: *
Disallow: /admin/
Disallow: /checkout/

Sitemap: https://hotel.example/sitemap-index.xml
```

The `Sitemap:` directive is independent of `User-agent` groups and applies to all crawlers that read `robots.txt`. Never place your sitemap file inside a directory blocked by a `Disallow` rule.

### 2. Submit in Search Engine Webmaster Tools

Submit your sitemap index URL directly in **Google Search Console** (under **Indexing > Sitemaps**) and **Bing Webmaster Tools** to track discovery counts, XML parse errors, and per-sitemap indexing coverage.

## Production Example: Hospitality Website

For a hotel or travel website, the sitemap should surface canonical landing pages for rooms, destinations, and promotional offers:

```xml title="sitemap.xml"
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://hotel.example/</loc>
    <lastmod>2026-09-20</lastmod>
  </url>
  <url>
    <loc>https://hotel.example/rooms/deluxe</loc>
    <lastmod>2026-09-19</lastmod>
  </url>
  <url>
    <loc>https://hotel.example/rooms/family-suite</loc>
    <lastmod>2026-09-18</lastmod>
  </url>
  <url>
    <loc>https://hotel.example/locations/da-nang</loc>
    <lastmod>2026-09-15</lastmod>
  </url>
  <url>
    <loc>https://hotel.example/offers/summer</loc>
    <lastmod>2026-09-20</lastmod>
  </url>
</urlset>
```

Keep dynamic booking queries and account routes **out** of the sitemap:

```text
❌ /booking/search?checkin=2026-10-01&checkout=2026-10-03
❌ /booking/confirmation/12345
❌ /account/reservations
❌ /admin/rooms
```

## Automating Dynamic Sitemaps in Application Code

Never maintain `sitemap.xml` by hand for CMS- or database-driven sites. Generate your sitemap dynamically from the exact same data source and routing logic that renders your canonical pages:

1. **Query only published, indexable records:** Filter by `status = 'published'` and `noindex = false` at the database/CMS query layer.
2. **Use the canonical URL helper:** Construct `<loc>` using the same canonical URL builder that powers your `<link rel="canonical">` tags so trailing slashes and slugs never diverge.
3. **Map `<lastmod>` to content update timestamps:** Read `updatedAt` from the CMS record rather than `new Date()`.
4. **Chunk automatically:** Paginate database queries into child sitemaps whenever a collection approaches 50,000 items.
5. **Cache with smart invalidation:** Serve the XML with `Content-Type: application/xml; charset=utf-8` and cache it at the CDN edge (`Cache-Control: public, max-age=3600, stale-while-revalidate=86400`), purging on CMS publish webhooks.

## Common Sitemap Anti-Patterns

| Anti-Pattern                                           | Why It Hurts                                                                                                 | Fix                                                                            |
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
| **Listing URLs blocked by `robots.txt` or `noindex`**  | Sends conflicting signals (`"please index this"` vs. `"do not crawl/index this"`) and triggers GSC warnings. | Filter out `noindex` and disallowed paths in your sitemap generator query.     |
| **Dumping facet/filter permutations into the sitemap** | Floods crawl budget with millions of near-duplicate URLs.                                                    | Include only base canonical category URLs and curated SEO landing pages.       |
| **Including `301` redirecting URLs**                   | Forces crawlers through unnecessary hops and flags `Page with redirect` in GSC.                              | Update the sitemap immediately whenever a slug changes or a redirect is added. |
| **Faking `<lastmod>` on every deploy**                 | Crawlers stop trusting your `<lastmod>` timestamps.                                                          | Bind `<lastmod>` strictly to the record's last content modification date.      |

## Pre-Deployment Checklist

- [ ] Every `<loc>` is an absolute canonical URL returning `HTTP 200 OK`.
- [ ] Zero URLs in the sitemap are blocked by `robots.txt`, marked `noindex`, or redirecting (`3xx`).
- [ ] XML is well-formed UTF-8 and served with `Content-Type: application/xml` (or `text/xml`).
- [ ] Each sitemap file stays under **50,000 URLs** and **50 MB** uncompressed.
- [ ] `<lastmod>` reflects true content modification timestamps in ISO 8601 format (`YYYY-MM-DD`).
- [ ] The root sitemap or sitemap index is declared via `Sitemap:` in `/robots.txt` and submitted in Google Search Console.
- [ ] Verify the live sitemap with the [Robots + Sitemap Inspector](https://nhanbe.id.vn/building-for-the-web/web-seo/robots-sitemap-inspector.md).

## References

- [Sitemaps XML Protocol Specification — sitemaps.org](https://www.sitemaps.org/protocol.html)
- [Build and Submit a Sitemap — Google Search Central](https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap)
- [RFC 9309 — Robots Exclusion Protocol](https://www.rfc-editor.org/rfc/rfc9309.html)
