Skip to content
Menu

Your sitemap can return a 404 with perfect XML inside

A WordPress sitemap can look flawless in the browser and still answer HTTP 404. Google does not use a sitemap that answers 404. Here is how to check yours in one minute, and how to fix it.

What the server was sending, measured on this site

GET /wp-sitemap.xmlHTTP/1.1 404 Not Found← the status Google readsContent-Type: application/xml<sitemapindex> … 74 addresses← perfectly valid

What we found

When we rebuilt this website, every sitemap page opened normally in the browser: a clean list of 74 addresses, valid XML, nothing missing. The server was sending all of it with the status code 404, "not found".

A person looking at the page sees no difference. Google does. A sitemap that answers 404 is treated as a sitemap that does not exist, however correct its content is.

Why it happens

WordPress decides whether a request is a 404 by looking at its main query. A sitemap request does not return posts, so on some sites WordPress marks it as "not found" before the sitemap is drawn. The XML still goes out, with the wrong status on top.

It tends to appear on sites with few or no blog posts, or where a plugin or theme changes how requests are resolved. We have seen the same defect on more than one site.

How to check yours

Browsers hide status codes, so check it directly. In a terminal, run curl -I https://yoursite.com/wp-sitemap.xml and read the first line. It must say 200. Do the same for one of the lists inside it, such as /wp-sitemap-posts-page-1.xml.

Then look at what Google says:

  • Open Google Search Console and go to Indexing, then Sitemaps.
  • Submit wp-sitemap.xml if it is not there yet.
  • The status should read "Success", with the number of pages discovered. "Couldn't fetch" or an HTTP error, while the file opens fine in your browser, is the sign of this problem.

How to fix it

The fix belongs in the pre_handle_404 filter: when the request is for a sitemap that exists, tell WordPress it is not a 404. These are the lines that fixed this site. They check that the sitemap really exists, so invalid sitemap addresses still answer 404:

add_filter( 'pre_handle_404', function ( $pre ) {
	$sitemap = get_query_var( 'sitemap' );
	if ( '' === $sitemap ) {
		return $pre; // not a sitemap request
	}
	if ( 'index' === $sitemap ) {
		return true; // the sitemap index always exists
	}
	// only sitemaps that really exist answer 200
	$server = wp_sitemaps_get_server();
	return $server->registry->get_provider( $sitemap ) ? true : $pre;
} );

Add it through a small plugin of your own or a code snippets tool, not the theme's functions.php, so a theme update does not remove it.

How to confirm it worked

  • Run the curl -I check again: the index and every list must answer 200.
  • In Search Console, paste the sitemap address into URL Inspection and choose "Test live URL". The page fetch must be successful.
  • Resubmit the sitemap and wait for the status to change to "Success".

Do not trust your own browser for this. On this site, the index and every list now answer 200, and Search Console reads all the addresses.

It is not only about Google

Bing and the crawlers behind AI assistants also use sitemaps to discover pages. A sitemap that answers 404 slows down how fast every one of them finds new or changed content.

Want us to check this on your site?

Get in touch