Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Fix Yoast’s WordPress SEO Sitemap 404 Error

Last updated on by
Follow WPBeginner on YouTube
How to Fix Yoast’s WordPress SEO Sitemap 404 Error

Having been downloaded over 1 million times, WordPress SEO by Yoast plugin is by far the best and most complete SEO plugin for WordPress. While it has given us no issues in the past, for some users it has been a pain. Recently one of our 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. In this article, we will show you how to fix the sitemap 404 error in WordPress SEO plugin by Yoast.

Update: It is important that you understand that this issue is most likely caused by poorly coded theme function or plugin. Yoast’s plugin works fine on our sites. Just wanted to be very clear about this.

First thing you should try is open your .htaccess file (you can actually do this from WordPress SEO plugin > Edit Files option) and simply add the following code in there:

# 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. Well in our client’s case, the above solution did NOT fix the issue.

While not preferred, 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 seems to fix the 404 issue on our client’s website. We have no idea why Yoast is not doing this by default, but it seems that quite a few users are having this sitemap 404 issue. This issue is explained in the comments.

Update: As 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 this issue, we hope that this fix has helped you.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Rami Alloush says:

    Uncheck then re-check indeed solved that for me.
    Thanks

  2. Nikos Kavvadas says:

    I add the code in the .htaccess and then i uncheck the sitemap option in the Yoast’s SEO plugin and save their settings. After that i re-checking the sitemap’s option and the the error is solved!

    Thanks!

  3. Henk Valk says:

    Thanks Joshua
    Had the same problem. Changing the rows from 1000 to 100 will fixed it for me.

  4. Morteza says:

    Thanks a lot, but not works for me. I do all of these ways and check/save/uncheck/save/XML Sitemap process after one & both of them… but ERROR 404!
    I’ll try another plugins for this.
    thanks for your great site..

  5. Gaurav k says:

    yeah this one is a great finding thanks a lot

  6. TimValDen says:

    Worked for me too… I had the 404page problem when loading the sitemap generated by Yoast SEO.

  7. Joshua Largent says:

    This is what worked for me – Go to the yoast XML sitemap on your dashboard. Change the Max Entries per sitemap from 1000 to 100. Worked like a charm for me. :)

  8. onarto says:

    It looks like htaccess edition disappear since yesterday… nothing in the menu anymore…

  9. Adrienne McCue says:

    Just checking and unchecking takes care of the issue. I will check this plugin out now! I did so much seo before, but I like so many features here!

  10. David Bell says:

    I found the same thing as Jane below. Thank you!

  11. smaug says:

    adding the following code to my .htaccess fixed the sitemap for me:

    https://gist.github.com/jdevalk/5345872

    (note this code is for if your wordpress installation is in a subdirectory named wordpress, if your is in a different folder, change the directory name in the code)

  12. Awais says:

    i have photography website . Should i uncheck the exclude media option or it’s ok?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.