How to Fix Yoast’s WordPress SEO Sitemap 404 Error

50

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.

Social Media Auto Publish Powered By : XYZScripts.com