Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Fix Yoast’s WordPress SEO Sitemap 404 Error

Are you getting a 404 error when trying to view your sitemap? If you’re using Yoast SEO, this can happen. In this article, we’ll show you how to easily fix Yoast’s SEO sitemap 404 error.

Yoast SEO is one of the most popular SEO plugins for WordPress, but sometimes it can conflict with other plugins or themes on your site.

One of our previous clients ran into the issue of getting a 404 error for their sitemaps generated by Yoast’s WordPress SEO plugin. After trying a few things, we were able to figure out the solution.

First thing you should try is open your .htaccess file. You can use a plugin like Htaccess File Editor to edit your htaccess file from your WordPress dashboard.

We recommend creating a backup of your WordPress site before making any changes, as even a small typo could bring down your website.

Once you’ve installed the plugin, go to Settings » WP Htaccess Editor. Make sure to read the instructions before you click the “I understand. Enable the editor” button.

enable the htaccess editor

After you enable the editor, simply add the following code in there, at the bottom:

# WordPress SEO - XML Sitemap Rewrite Fix
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
</IfModule>
# END WordPress SEO - XML Sitemap Rewrite Fix

For most people this fixed the issue. Someone reported that while this code fixed the issue, the page was still sending the 404 response header which meant that Google Webmasters tool couldn’t find it.

If that doesn’t fix the 404 sitemap issue, you may need to try another method. While not preferred, in our client’s case we had no choice but to edit the core plugin files to fix the issue. As suggested by hadjedj.vincent, we edited the class-sitemaps.php located in “/wp-content/plugins/wordpress-seo/inc/class-sitemaps.php”.

You need to look at the function init() code and make that section look like this:

/**
	 * Initialize sitemaps. Add sitemap rewrite rules and query var
	 */
	function init() {
		global $wp_rewrite;
		$GLOBALS['wp']->add_query_var( 'sitemap' );
		$GLOBALS['wp']->add_query_var( 'sitemap_n' );
		add_rewrite_rule( 'sitemap_index.xml$', 'index.php?sitemap=1', 'top' );
		add_rewrite_rule( '([^/]+?)-sitemap([0-9]+)?.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );
		$wp_rewrite->flush_rules();
	}

Basically what we are doing is adding the global $wp_rewrite; before everything that is already there and adding flush_rules after the code. By adding this in the plugin, it fixed the sitemap 404 issue on our client’s website.

Some of our users reported that after applying the above fix, they still needed to uncheck the sitemap option in the Yoast’s SEO plugin and save their settings. After that re-checking the sitemap’s option fixed the error for them.

If you were looking for a fix for your sitemap 404 issue, we hope that this fix has helped you! You might also want to check out our comparison of Yoast SEO vs. AIOSEO.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

240 CommentsLeave a Reply

  1. Hi, just tried the second version editing the plug ins core files and the site has disapeared?? including the backend. just a white screen?? I replaced the code withy the original and nothing.

  2. Thank you! That worked by supplying the code in the .htaccess file, then unchecking the site map option and saving. Then, re-checking the box and saving again. Thanks!

  3. After loosing quite some time trying to find what was wrong, I got it working by your suggestion “Uncheck the sitemap option in the Yoast’s SEO plugin and ‘save’ settings. After that re-checking the sitemap’s option fixes the error.”
    tnx very much!

  4. After I added the code to htaccess and unchecked/rechecked Sitemap in the plugin, it worked! Thanks!

  5. Uncheck then save, then go back check again and save, solved my problem. It should be at the begining before anybody try anything else! it worked for me thanks though

  6. Woohoo! Thanks. Mine probably wasn’t broken long because Google didn’t seem to mind yet. … But my site is only in a softer launch stage anyway… Slow crawls maybe. Luckily, I’m improving my site before my real projects go up (hopefully soon).

    Anyway I cannot thank you enough. :)

  7. I opened the class-sitemaps.php file but can’t find the section you are referring to “functiion () init code.. can you be more specific?

  8. Editing the .htaccess file, un-checking sitemap generation, saving settings, re-checking, saving settings … fixed it for me! Thank you VERY much.

  9. I found my issue was actually fixed purely by turning Yoasts sitemaps on and off from UI. Which is great! :)

  10. Were you able to fix this one? I’m having the same problem, I get a 404 on post-sitemap.xml. I didn’t check the PHP logs on what the error is but probably would be the same you’re getting.

    • No fix, and theres no replies to support threads on wordpress, other people are just disabling it and using another sitemap plugin.

  11. I was having an issue with the sitemap 404’ing when trying to access it.

    I tried adding the first ifmodule, no joy, then remembered I had another ifmodule in place to handle redirects after updating my permalink structure.

    I removed the redirect ifmodule, restarted Worpress SEO, then the sitemaps started working. Re-added the redirect ifmodule, and everything now appears to be working just fine. I’ve also checked the status in GWT and Bing WT and it’s all looking good. Thanks for the help!

  12. I put the first Ifmodule into htaccess and it spit out some weird single line of content, with no links.

    I read through the comments

    then I rebooted Yoast’s sitemap option:

    i unchecked the “Enable XML Sitemap Here” and saved

    I re-checked it and saved

    checked my sitemap and it was there, formatted properly with xsl stylesheet being applied and links all worked

  13. I have no idea where to put that code. There is no line like: “function init()” in class-sitemaps.php

  14. Try going into your permalink settings and saving them again. If you can’t access the back end of your site because of the 500 error, then remove that htaccess file.

  15. Hello! I think you should add one more step at the very beginning: Re-save your permalink settings! I found that re-saving my permalink settings solved the issue for me. No need to go into the htaccess file.

  16. I get the following error only on post-sitemap, all other sitemaps work ok:
    Fatal error: Allowed memory size../plugins/wordpress-seo/inc/class-sitemaps.php on line 713
    I tried the htaccess and wpseo-functions edits but no luck.
    I deactivated every plugin, still no luck.
    I deleted wpseo and reinstalled but still same problem and now on the wpseo dashboard and sitemaps page I get this error:
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘wpseo_xml_sitemaps_init’ not found or invalid function name in /home/justplay/public_html/wp/wp-includes/plugin.php on line 470

    I have two sites running the same theme, plugins etc and one works ok one has this errors, wierd.

  17. Thank you – i found just by unchecking and saving and then re-checking the site map option for the yoast plugin google webmaster was able to find my site map

  18. When I tried the htaccess method, it crashed my site. I just added this code into the htaccess file (via the Yoast plugin):

    # WordPress SEO – XML Sitemap Rewrite Fix

    RewriteEngine On
    RewriteBase /
    RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
    RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]

    # END WordPress SEO – XML Sitemap Rewrite Fix

    And then clicked the “save changes to .htaccess” button. I can’t go anywhere without the 500 error now. What did I do wrong? Anyone know how to fix this?

  19. So it still doesn’t work for me… I feel like beating my head against a wall. As much as I love the plugin, I think I’m just going to lose it in favor of something that’s actually functional.

  20. @Matt Klein, the ‘init’ function is now ‘wpseo_xml_sitemaps_init’ in the wpseo-functions.php, @line 461

    Add just :
    global $wp_rewrite; before the $GLOBALS define
    and
    $wp_rewrite->flush_rules(); after the add_rewrite_rule…

    /**

    * Initialize sitemaps. Add sitemap & XSL rewrite rules and query vars

    */

    function wpseo_xml_sitemaps_init() {

    $options = get_option( ‘wpseo_xml’ );

    if ( $options[‘enablexmlsitemap’] !== true ) {

    return;

    }

    // redirects sitemap.xml to sitemap_index.xml

    add_action( ‘template_redirect’, ‘wpseo_xml_redirect_sitemap’, 0 );

    if ( ! is_object( $GLOBALS[‘wp’] ) ) {

    return;

    }

    global $wp_rewrite;

    $GLOBALS[‘wp’]->add_query_var( ‘sitemap’ );

    $GLOBALS[‘wp’]->add_query_var( ‘sitemap_n’ );

    $GLOBALS[‘wp’]->add_query_var( ‘xsl’ );

    add_rewrite_rule( ‘sitemap_index.xml$’, ‘index.php?sitemap=1’, ‘top’ );

    add_rewrite_rule( ‘([^/]+?)-sitemap([0-9]+)?.xml$’, ‘index.php?sitemap=$matches[1]&sitemap_n=$matches[2]’, ‘top’ );

    add_rewrite_rule( ‘([a-z]+)?-?sitemap.xsl$’, ‘index.php?xsl=$matches[1]’, ‘top’ );

    $wp_rewrite->flush_rules();

    }

  21. There is no function init area in my class-sitemaps.php file. Could it be called something else. What line is it on?

  22. Htaccess fix worked for me.

    But as Scott in the first comment above says, I needed to uncheck the XML sitemap checkbox and save. Then recheck it and save again for the sitemap to show up. I think that would be worth mentioning in the article.

  23. Thanks this worked for me editing the .htaccess file. I unchecked sitemaps off then back on and also changed mine from 1,000 to 400. If you are on a shared server, I would use a lower number so that less CPU usage is done on your domain.

  24. Hi guys,

    I had to troubleshoot this issue too. My site is hosted in a sub directory of a main site. So I had to set the following line:

    RewriteBase /scottmortiz/

    I then had to uncheck the mina Site Map XML option in the plugin, and save. Then re-check the plugin and save. Hope this works for some of you.

    -Scott

  25. Hi Steven,
    thank alot. This helped me, too. But to get the rewrite working I had to disable the xml function, save it, and enable (and save it) again. Seems obvious, but maybe this is worth a note in the article. I will safe this article in my personal wordpress help register.
    Thanks,
    Chris

    • Ah, switch it off, then switch it back on again. Thanks:-) It is not as obvious as you think. My sitemaps also did not work (getting a 404) until I did this. I did not have to make any of the other changes described in this post.

  26. The edit to .htaccess files was just the trick. Thanks for posting this! You saved me a whole lot of time and trouble.

  27. I was just facing the same issue. i unticked the box (Check this box to enable XML sitemap functionality) — pressed save, and then ticked the box again, after that It found the sitemap again, Hope it will be there!

  28. I’ll just share what worked for me, by changing the ‘max entries per site map’ to 250 (ie. something less than 1000) I managed to fix the 404 error. I think the server was having issues processing 1000 entries which is the default.

    • Hi Steven, this is great solution! I just fixed the problem by your method. Thank you very much!

    • Thanks Steven – this resolved my issue as well. The ‘max entries…’ field was blank for me; simply adding any value seemed to work, which is good as I felt 250 was a bit low. Very simple but effective solution.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.