Our sitemap index used to promise 14 product shards. The catalogue only filled 6. For two weeks we were telling Google that eight XML files existed, and returning 404 for every one of them — several times a day, from Mountain View and from Singapore.
The 404 itself was correct. The URL genuinely did not exist. What was wrong was the invitation.
How a sitemap shard becomes a dead URL
WordPress core's sitemap provider paginates by a hardcoded query limit. When the catalogue had 13,403 published products, the provider happily generated product-1.xml through product-7.xml. We then shipped a quality filter that removed template-cloned listings from the sitemap, the catalogue dropped to 10,941, and the shard count fell to 6.
Nothing told the crawler that the last eight shards had retired. A sitemap is a snapshot, not a contract, but crawlers keep the old snapshot in a queue for a while. So they knock on doors that are no longer part of the house.
Our server logs for one day: 5 hits on retired shards, all 404. Not a catastrophe. But a 404 is an ambiguous answer. It says "maybe you typed it wrong." A 410 says something different and useful: this was here, it is gone on purpose, stop asking.
Why not just nginx?
The obvious fix is eight location blocks returning 410. We did not do that, for a reason that bites in about a month.
product-7.xml is only invalid today. When the catalogue crosses back over the shard threshold — and with 26,000+ SKUs cycling through publish/unpublish, it will — shard 7 becomes legitimate again. A hardcoded 410 in nginx would then serve "gone" for a sitemap that has 1,900 live products in it. We would have traded a transient 404 for a permanent, silent de-indexing of a whole shard.
So the rule lives in PHP, where it can see the catalogue.
add_action('template_redirect', function () { $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); if (!preg_match('~^/wp-sitemap-posts-([a-z_]+)-(\d+)\.xml$~', $path, $m)) { return; } $type = $m[1]; $page = (int) $m[2]; $provider = wp_sitemaps_get_server()->registry->get_provider($type); if (!$provider) { status_header(410); exit; } $data = $provider->get_sitemap_type_data(); $pages = isset($data['pages']) ? (int) $data['pages'] : 1; if ($page > $pages) { status_header(410); header('Content-Type: application/xml; charset=UTF-8'); exit; // zero-byte body on purpose } }, 1); Two details that cost us an hour each.
First, get_max_pages() does not exist in the WordPress version we run. The old API was get_max_pages(); the current provider exposes get_max_num_pages() and get_sitemap_type_data(), and only the latter gives you the per-type page count without re-running the query twice. Calling a method that is not there throws inside template_redirect, which nginx turns into a 500 — and a 500 on a sitemap shard is worse than the 404 you were trying to improve.
Second, send no body. A 410 with an HTML error page in it is 2 KB of noise that the crawler has to fetch and discard. Empty body, XML content type, done.
What the log looks like now
| Path | Before | After |
|---|---|---|
product-6.xml | 200 | 200 (134,841 bytes) |
product-7.xml | 404 | 410, 0 bytes |
product-8.xml | 404 | 410, 0 bytes |
product-11.xml | 404 | 410, 0 bytes |
product-14.xml | 404 | 410, 0 bytes |
post-1.xml | 200 | 200 |
brands-1.xml | 200 | 200 |
taxonomies-product_cat-1.xml | 200 | 200 |
The guard is scoped to the shard pattern only. Category sitemaps, brand sitemaps and the index itself pass through untouched, because a false 410 there would be a real visibility loss and those counts move daily.
The part that generalises
If you paginate anything — sitemap shards, archive pages, API result pages — the page count is a function of your data, and your data changes. Any rule that encodes "page N is dead" as a static config is a rule that will be wrong later, silently, in the direction that costs you traffic.
Put the boundary check where the count is known. For us that is a provider object that reads the same query the sitemap itself uses. It costs one regex and one integer comparison per request that matches the pattern, and it self-heals.
We also left the old 404 behaviour for everything that is not a shard. A 404 on /product-category/bags/page/27/ — that category has 26 pages, we checked — is genuinely "this was never here," and 404 is the honest answer there.
One more thing we learned from the same log review: 246 of the day's 258 404s were vulnerability probes (/.env, /.git/config, /wp-config.php.bak). Those do not need fixing, they need ignoring, and they will make your 404 graph look catastrophic if you do not filter them out before you panic.
The catalogue those shards describe is here if you want to see what 10,941 product URLs actually look like in a sitemap that is not lying to anyone: designer bags, shoes, and the buying guides that link into them. Return policy is 30 days, duties prepaid, orders go through WhatsApp.