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.
Yoast插件WordPress SEO已被下载超过100万次,是迄今为止最好,最完整的WordPress SEO插件。 尽管过去它没有给我们带来任何问题,但对于某些用户而言,这一直很痛苦。 最近,我们的一位客户遇到了Yoast的WordPress SEO插件生成的站点地图出现404错误的问题。 在尝试了几件事之后,我们能够找到解决方案。 在本文中,我们将向您展示如何通过Yoast修复WordPress SEO插件中的Sitemap 404错误。
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.
更新:重要的是要了解此问题最有可能是由主题功能或插件的编码不正确引起的。 Yoast的插件在我们的网站上可以正常工作。 只是想对此很清楚。
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:
首先,您应该尝试打开.htaccess文件(实际上可以从WordPress SEO插件>“编辑文件”选项中执行此操作),然后在其中简单地添加以下代码:
# 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.
对于大多数人来说,这解决了这个问题。 有人报告说,虽然这段代码解决了该问题,但该页面仍在发送404响应标头,这意味着Google Webmasters工具找不到它。 在我们客户的情况下,上述解决方案无法解决问题。
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”.
虽然不是首选,但我们别无选择,只能编辑核心插件文件来解决此问题。 根据hadjedj.vincent的建议,我们编辑了“ /wp-content/plugins/wordpress-seo/inc/class-sitemaps.php”中的class-sitemaps.php 。
You need to look at the function init() code and make that section look like this:
您需要查看函数init()代码,并使该部分如下所示:
/**
* 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.
基本上,我们正在做的是添加全局$ wp_rewrite;。 在已经存在的所有内容之前添加代码,并在代码之后添加flush_rules。 通过在插件中添加此代码,似乎可以解决客户网站上的404问题。 我们不知道为什么Yoast默认不这样做 ,但似乎很多用户都遇到了此站点地图404问题。 注释中解释了此问题。
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.
更新:正如我们的一些用户报告的那样,在应用上述修复后,他们仍然需要取消选中Yoast SEO插件中的sitemap选项并保存其设置。 之后,重新检查站点地图的选项可以解决该错误。
If you were looking for this issue, we hope that this fix has helped you.
如果您正在寻找此问题,我们希望此修复程序对您有所帮助。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-fix-yoasts-wordpress-seo-sitemap-404-error/