Having been downloaded over 1 million times, the WordPress SEO by Yoast plugin is the best and most complete SEO plugin for WordPress. While it hasn't given us any problems 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 the Yoast WordPress SEO plugin. After trying a few things, we were able to find the solution. In this article, we will show you how to fix 404 sitemap error in Yoast WordPress SEO plugin.
Update:It is important for you to understand that this issue is most likely caused by a poorly coded plugin or theme feature. The Yoast plugin works fine on our sites. I just wanted to be very clear about that..
The first thing to try is to open your .htaccess file (you can do this from the WordPress SEO plugin> Edit Files option) and simply add the following code there:
# WordPress SEO - XML Sitemap Rewrite Repare 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
For most people this fixed the problem. Someone reported that while this code fixed the issue, the page still sent the 404 response header, which meant the Google Webmasters Tool couldn't find it. Well, in our client's case, the above solution did NOT fix the problem.
While not preferred, we had no choice but to edit the core plugin files to fix the issue. As suggested by hadjedj.vincent, we edit the class-sitemaps.php located at "/wp-content/plugins/wordpress-seo/inc/class-sitemaps.php".
You need to look at the code for the init() function and make that section look like this:
/ ** * Inicializar sitemaps. Agregue reglas de reescritura de sitemap y consulte 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  & sitemap_n = $ matches ', ' '); $ wp_rewrite-> flush_rules ();
Basically what we're doing is adding the global $wp_rewrite; before everything is already there and add flush_rules after the code. Adding this in the plugin seems to fix the 404 issue on our client's website. We have no idea why Yoast isn't doing this by default, but it seems a lot of users are having this 404 sitemap issue. This issue is explained in the comments.
Update: As some of our users reported that after applying the above solution, they still had to uncheck the sitemap option in the Yoast SEO plugin and save their settings. After rechecking the sitemap option, the error was fixed.
If you were looking for this problem, we hope this solution helped you.