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.
If you were looking for this issue, we hope that this fix has helped you.







Thanks for the fix
Thank you so much!
this worked — had to make both changes, (edited the class-sitemaps.php file).
Thanks a lot!
It worked
Damn helpful.
One of my clients had this problem and just took 5 minutes to solve the issue. Just because of this post.
Thanks a lot.
I am new to WordPress and have only begun a using the Canyon theme. I got the 404 message as I was attempting to install a “contact info” plugin. Does anyone know if I just begin at the very beginning again, i.e. go through my GoDaddy account to reinstall WordPress and then reinstall the Canyon theme – might that solve the 404 problem?
It is probably the issue with that other plugin.
Thanks for the fix!
I have two sets of websites (WordPress Multisite installs) and I have no issues with sitemaps on one of them and I get a 404 error with category_sitemap.xml on the other one.
I tried both fixes suggested in this post, but they don’t solve the issue. The only thing that works is going to Posts -> Categories – Edit and changing from “Auto detect” to “Always include” on all categories.
The issue here is that I have over 200 sites, and about 8 categories per page, so it would take too long to do it just to get category sitemaps, even though I could use it….
Is there any way to mass change all the categories on WP Multisite to “Always include”. I think this would be a solution that would work for everyone.
Thank you.
If none of this works try create a file named sitemap_index.xml on your html root through ftp and make sure the permissions are set and the plugin should then take care of the rest.
Thanks!! your suggest worked perfectly!
I did both and it started working. It sounds pretty obvious, but make sure if you copy and paste that you take the line numbers out. I forgot to and broke my .htaccess file for a few minutes.
Hi, just wanted to give my thanks, used the second option suggested ad it fixed my 404 error. Thanks!
I’m not sure which part worked for me, but I found this page because I was getting the 404, too. I disabled the XML Sitemaps in the plugin, then enabled it and checked the boxes to exclude posts (since I only have pages). Now, it is working perfectly.
Thanks Mate Perfect ! Suggestion for Me !!!!!!!!!!!!!!!!!! Really Greatful Thanks
During research about a similar 404 sitemap xml feed problem, I found a different reason which I would like to share:
WordPress adds a 404 header to all feeds if no posts exist in a site.
More details and a hotfix here:
http://wordpress.org/support/topic/sitemap-xml-feed-is-shown-but-404-header-added-by-wordpress-if-site-has-no-posts
I think there are other issues with this plugin as well.
The attachment pages aren’t all redirecting to the parent page which is something i noticed today.
Create a ticket in the support forum. We haven’t tried that feature yet.
No other comment since April. That has been 5 months since. I have the 404 still in Sept 2012! Was able to fix it with the .htaccess fix. Thanks for posting
Just wondering why this is STILL an issue.
It worked for me! Open the class-sitemaps.php file located in plugins > wordpress-seo > inc folder. then find the code around line 79 and replace the old code with this new code:
/**
* 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();
}
Then, go back to your WP admin page and then go to the SEO plugin > XML sitemaps, and then click the button again (XML Sitemap). The 404 should be replaced with your sitemaps! Easy peasy.
@SANDIE WRIGHT: I followed your instructions but it did not solve my 404 problem.
thanks you, the rewrite rule itself cured the problem of 404 not available sitemap
Thanks for jacking up my site! i tried your code and now i got a 500 internal service error i have been trying for hours to fix! thanks for making my little problem into a HUGE problem!
If you are getting a 500 internal service error, you can simply undo the changes. Not sure why you would get that.
I’m finding my sitemap gets removed everyday and then indexed again. Here’s what i get every day. http://postimage.org/image/3rb3d8qsr/932c5815/
I don’t think it’s my theme as i’m using Genesis.
Not sure but it may be the way my server is configured.
Has to be a conflict with something else (another plugin or server settings). We are using Genesis on this site as well and do not have that issue.
Hi!
Thank you so much for posting this article! With the first fix, I managed to solve the 404 issue for most sitemaps, though there’s still 1 that is showing the error… Any suggestions of what to do?
I thought about exporting the Yoast SEO settings and reinstalling the plugin to see if that does the trick. Just to be on the safe side: does exporting literally export all settings, even the page/article level SEO customizations OR only the general settings?
Thanks!
Eva
Thanks so much – this worked for me.
I know you said the plugin is good, and the problem is in the theme or other plugins, but I tried disabling all plugins and switching to the twenty eleven theme, and the sitemap still gave a 404 error. The only thing that worked for me is editing the htaccess file.
… and I tried the same thing. Removed all the plugins, all the themes, and tried it again: 404 error. To make it worse: I’d have an .htaccess file because of IIS. Which options are there for me?
Typo: ‘I’d have an’ must be ‘I don’t have an htaccess file’
I guess SEO Yoast recent update have sitemap problem as I cant see any sitemap on the URL the plugin provide. Let me know if any one has the same issuee and Fix.
Also is there any good plugin avaialble as same as SEO Yoast.
Thanks & Regarsd
Hi, I have tried the first fix, but broke my site in the process. Is there anyway to re-edit my htaccess file, to stop it causing a 500 internal server error?
thanks
You can simply take the code out. Use FTP <
Hi!
I have edited the plugin as suggested but don’t have a .htaccess file. I still do receive a 404 error.
I’ve also add message on the plugin forum. No replies yet. Suggestions are more than welcome!
http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-404-when-viewing-xml-sitemap-1
Hi Joots, I’m using the latest version of the SEO plugin. I’m going over previous posts and completeing the various fields, “Focus Keyword”, “SEO Title”, etyc, etc but after I update the post and then go back to “Edit” those fields are then blank again. I wondered how I could rectify this?
Thanks,
Colin
Liverpool, UK
The solution doesn’t resolved my issue.. I still have a blank page at post-sitemap.xml
Does someone have a solution?? Or I need to unistall this plugin and find another one for my sitemap..
Hi, I don’t have any problems with yoast seo plug in as I’m currently using it. I just I want to ask if it is better than the all in one seo plug in? I’m just curious because I found out that a lot of blogs in my niche uses it. Which is better? the all in one seo or yoast seo? (I’m already used to yoast). Oh, can I also ask how to set multiple focus keyword in Yoast seo? because there is only one field and I can’t figure out how to set a few more keywords for it. Sorry for my questions I hope you can answer me. Thanks!
Yoast’s plugin is a more complete SEO plugin.
Anyone here who’s willing to test another (and better) fix with me?
I’d be willing to test a better fix for this issue but I’m reluctant to commit to a timeframe as I’m not really good at keeping on top of my todo’s since my stroke. (This is one of the reasons I haven’t returned to work as a PHP coder yet.)
And I’d be *very* interested in helping if I happen to be experiencing any of the issues cited above.
Craig
Hi,
Yoast, if the offer is still good, I would LOVE too! I am not to savvy with all of this but would be willing to allow you to guide me
Sure, send me an email!
thanks..i use wordpress seo,but not with sitemap,because have no idea to fix this error
Hmm, I think the second part is going to cause the rewrite rules to be flushed on every page load, which <a href=”http://codex.wordpress.org/Rewrite_API/flush_rules”>the Codex</a> warns not to do due to performance issues:

<blockquote>Because this function can be extremely costly in terms of performance, it should be used as sparingly as possible – such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.</blockquote>
I’m guessing that’s why Joost isn’t doing that by default
Of course, I could be wrong because I don’t know his plugin well enough.
I was under the same impression when this article came through my RSS. I’m pretty sure going to your permalinks settings page would accomplish the same thing without having to flush the rewrite rules every time the page loads.
@jonbish Unless you’re running a crappy plugin or theme that DOES flush on every pageload before this plugin gets a chance to get its rewrites in.
@yoast @jonbish Yes we understand that this is probably a compatibility issue. Yoast sitemaps work fine on our sites. We had to do this on a client’s site.
@wpbeginner @jonbish I know YOU understand, question is: do your readers
We’re working on a fix for these kinds of issues in core btw: http://core.trac.wordpress.org/ticket/18450
@yoast @jonbish Added a note at the beginning of this article, so users understand as well
You’re 100% right Stephen. Something else *is* doing it on each pageload, which forces this “fix”, which isn’t a fix but a workaround for a broken theme or other plugin…
And now? It seems a lot of people can’t use your plugni as intended.
Thanks! I was about to switch to All-In-One because I kept getting sitemap issues.
Do you know what to do when the sitemap returns a blank page?