Sub topic 12
Telling crawlers where your sitemaps are
A sitemap only helps if crawlers know it exists. This chapter covers the ways to publish and submit one: the Sitemap: line in robots.txt, search engine webmaster tools, and the scope rules that decide which URLs a sitemap may list. It assumes you can read a basic robots.txt file and a sitemap or sitemap index file. It also assumes you know what an HTTP status code is.
The three discovery routes
Crawlers learn about a sitemap in one of three ways:
- A
Sitemap:line in robots.txt. Every crawler that reads yourrobots.txtsees it. This route needs no account with any search engine. - Submission in a webmaster tool. Google Search Console and Bing Webmaster Tools both have a sitemaps report where you enter the sitemap URL. You also get processing status and error messages.
- Direct requests. Some tools and crawlers fetch a conventional path such as
/sitemap.xmlwithout being told. Don't rely on this. The path is a convention, not a standard.
Use the first two together. The robots.txt line is for every crawler. The webmaster tool gives you feedback about what the search engine did with the file.
The Sitemap directive in robots.txt
The Sitemap directive takes one absolute URL. Add one line per sitemap file, or a single line that points to a sitemap index.
User-agent: *
Disallow: /admin/
Sitemap: https://www.example.com/sitemap_index.xml
Sitemap: https://www.example.com/news/sitemap-news.xmlSome details to know:
- The URL must be absolute. It needs the scheme and host.
Sitemap: /sitemap.xmlis not valid. - The line does not belong to a group. It is independent of any
User-agentgroup, so it applies to all crawlers wherever you place it. Putting it at the end, separated by a blank line, makes that obvious to human readers. - You can repeat it. Several
Sitemap:lines are allowed. If you use an index file, one line is usually enough. - The field name is case-insensitive. The URL itself is case-sensitive, like any URL path.
- The sitemap URL is not affected by
Disallow. Don't block the sitemap path anyway. A rule such asDisallow: /*.xml$would stop crawlers from fetching your sitemaps, and this is a common mistake.
The robots.txt file must still live at the root of the host, such as https://www.example.com/robots.txt. A Sitemap: line inside https://www.example.com/blog/robots.txt is never read.
Reading the directive with Python
The standard library parser exposes the sitemap lines through site_maps(), available from Python 3.8. It returns a list, or None when no Sitemap: line exists.
from urllib.robotparser import RobotFileParser
rules = [
"User-agent: *",
"Disallow: /admin/",
"",
"Sitemap: https://www.example.com/sitemap_index.xml",
"Sitemap: https://www.example.com/news/sitemap-news.xml",
]
rp = RobotFileParser()
rp.parse(rules)
print(rp.site_maps())The output is:
['https://www.example.com/sitemap_index.xml', 'https://www.example.com/news/sitemap-news.xml']Submitting in a webmaster tool
In Google Search Console, open the Sitemaps report for a verified property and enter the sitemap path. The report lists each submitted sitemap with its last read date, a status, and the number of discovered pages. Bing Webmaster Tools has a similar Sitemaps page.
A submission has three benefits over the robots.txt line alone:
- You see whether the file was fetched and parsed, with specific error messages.
- You can submit a sitemap that is not listed in
robots.txt, for example to test a new one. - You get per-sitemap counts, which help when you split sitemaps by section. A sitemap for products and another for blog posts let you see which section has problems.
Submitting does not guarantee that any URL is crawled or indexed. A sitemap is a hint about which URLs you care about. Chapter 8 covered what a sitemap does and does not do.
Pinging is retired
Older guides tell you to request https://www.google.com/ping?sitemap=... after each update. Google retired its ping endpoint in 2023, and the request no longer has any effect. Bing also stopped accepting sitemap pings and points site owners to the IndexNow protocol for instant change notifications. For Google, the dependable signals are an accurate lastmod and a crawlable sitemap that is declared in robots.txt or submitted in Search Console.
Scope: which URLs a sitemap may list
The location of a sitemap file limits the URLs it may contain. A sitemap at https://www.example.com/blog/sitemap.xml may list only URLs that start with https://www.example.com/blog/. A sitemap at the root of the host may list any URL on that host.
The scope also respects the scheme and host exactly:
| Sitemap location | URL listed inside | Allowed by default |
|---|---|---|
| `https://www.example.com/sitemap.xml` | `https://www.example.com/shop/item1` | Yes |
| `https://www.example.com/blog/sitemap.xml` | `https://www.example.com/shop/item1` | No, outside `/blog/` |
| `https://www.example.com/sitemap.xml` | `http://www.example.com/shop/item1` | No, different scheme |
| `https://www.example.com/sitemap.xml` | `https://example.com/shop/item1` | No, different host |
| `https://cdn.example.net/sitemap.xml` | `https://www.example.com/shop/item1` | Only with cross-submission |
For these reasons, put the sitemap at the root and name it for the whole host. Splitting by section is better done with an index file at the root and child files named after the section, such as /sitemap-blog.xml.
Cross-submission
A sitemap can be hosted on a different host if the site that owns the URLs authorizes it. One way is to reference the external sitemap from the robots.txt of the host whose URLs it lists. Here www.example.com/robots.txt lists a file hosted on a CDN:
User-agent: *
Disallow:
Sitemap: https://cdn.example.net/sitemaps/example-com.xmlThe other way is to submit the sitemap in a webmaster tool where you have verified ownership of the site it describes. Both amount to the same proof: you control the host whose URLs are listed.
File requirements that affect discovery
A crawler that finds the URL still has to fetch and parse the file. Check these before you blame the crawler:
- The server returns HTTP 200. A redirect chain, a 404, or a 5xx response means the sitemap is not read. A single redirect from
httptohttpsis a common cause of "couldn't fetch" reports when therobots.txtline still names thehttpURL. - The file is UTF-8 and well-formed XML. A single unescaped
&in alocvalue breaks the file. - The
Content-Typeis something XML-like, such asapplication/xmlortext/xml. Compressed files may be served as.xml.gz. The size limits apply to the uncompressed content. - The sitemap is not behind a login, a firewall rule, or a bot-protection challenge that blocks crawlers.
- The sitemap URL matches the canonical host. If your site uses
www, theSitemap:line should too.
Checking your own site
A short script helps you audit the declared sitemaps on a live site. It reads robots.txt, then sends a request to every Sitemap: URL and prints the status code. It uses GET, because some servers reject HEAD requests for XML files.
import urllib.error
import urllib.request
from urllib.robotparser import RobotFileParser
def check_sitemaps(site):
rp = RobotFileParser(f"{site}/robots.txt")
rp.read()
sitemaps = rp.site_maps() or []
if not sitemaps:
print("No Sitemap lines found in robots.txt")
return
for url in sitemaps:
try:
with urllib.request.urlopen(url, timeout=10) as resp:
kind = resp.headers.get("Content-Type", "unknown")
print(resp.status, kind, url)
except urllib.error.HTTPError as err:
print(err.code, "HTTP error", url)
except urllib.error.URLError as err:
print("ERR", err.reason, url)
check_sitemaps("https://www.example.com")Replace the example host with your own. urlopen follows redirects silently, so also run curl -sI https://www.example.com/sitemap_index.xml when you want to see the redirect status itself.
Common discovery problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Search Console reports "Couldn't fetch" | Non-200 response, firewall block, or wrong host | Request the URL with `curl -I`, then fix the status or the declared URL |
| Sitemap is read but zero URLs are accepted | URLs are outside the sitemap's scope, or use another host or scheme | Move the file to the root, or correct the `loc` values |
| Crawlers ignore the `Sitemap:` line | The `robots.txt` is not at the host root, or it returns an error | Serve it at `/robots.txt` with a 200 status |
| New sitemap never appears | It is not declared and not submitted | Add the `Sitemap:` line and submit it in the tool |
| Sitemap fetch blocked | A `Disallow` rule matches the sitemap path | Remove or narrow the rule |
Exercise
A site serves https://shop.example.com/robots.txt with the line Sitemap: /sitemaps/main.xml. Search Console shows no sitemap read from this file. Give two things that are wrong or worth checking, and write the corrected line.
Show answer
The URL is relative, and the Sitemap directive requires an absolute URL including scheme and host. Also check that https://shop.example.com/sitemaps/main.xml returns HTTP 200 with well-formed XML, and that no Disallow rule matches /sitemaps/. The corrected line is Sitemap: https://shop.example.com/sitemaps/main.xml.
Exercise
You host a sitemap at https://www.example.com/blog/sitemap.xml. It lists https://www.example.com/blog/post-1 and https://www.example.com/shop/item-1. Which URL is outside the sitemap's default scope, and how would you fix it?
Show answer
The /shop/item-1 URL is outside the scope, because a sitemap in /blog/ may list only URLs that begin with /blog/. Move the sitemap to the root as https://www.example.com/sitemap.xml, or put the shop URLs in a separate sitemap at a location that covers them, and reference both from a sitemap index at the root.