• Installed a new site, it has only pages, no posts.

    Also installed the WordPress Sitemap Generator Plugin with a modified template for the pages.

    Testing example.com/sitemap.xml in browser showed the xml with all the pages, all seemed fine.

    But then Google Webmaster Tools complained about a 404 not found for the /sitemap.xml url.

    Some testing showed that WordPress adds a 404 header to all feeds, including the sitemap xml feed, if no posts exist in the website.

    Simple workaround would be adding a post with some content to the site, even if it is not linked anywhere.

    A better workaround would override the 404 somehow and after some research I used this code for functions.php for now:

    function my_404_fix() {
    	global $wp_query;
    	if (is_feed() && $wp_query->query_vars['feed'] == 'sitemap') {
    		status_header(200);
    		$wp_query->is_404=false;
    	}
    }
    add_filter('template_redirect', 'my_404_fix');

    Is there a better solution? Can this behaviour be considered as a bug in WordPress?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thanks for the great tip! I was pulling my hair out for hours trying to figure out why one of my MS sites was fine and the other was giving a 404… even though the page was visible.

    The site in question was made up of pages and no posts.

    Thanks

    DP

    This fix also worked for my site that have posts and pages, hosted on nginx. Sitemaps (created with Yoast WordPress SEO plugin) were visible in browser but when you examine response headers with Firebug, they were shown as 404 Not Found. (Rewrite rules were applied.)

    Noted here for someone else may having this problem with same tools.

    Here comes a correction: At first I thought it worked, because started to show 200 OK headers randomly, but not related the hotfix above, not sure if it was browser cache or something else.

    I found the source of 404’s though, it’s Batcache plugin. I added a line to disable it for site map URL (just for parent site map (sitemap_index.xml) was enough) and it worked. Like this:

    if( strpos( $_SERVER['REQUEST_URI'], 'sitemap_index.xml' ) )
    	return;
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sitemap xml feed is shown but 404 header added by WordPress if site has no posts’ is closed to new replies.