解决WordPress SEO插件XML站点地图404报错的问题

1 星2 星3 星4 星5 星 (1 votes, average: 4.00 out of 5)

自从博客换到独立主机以后,装了WordPress,又装了几个插件,其中之一就是WordPress SEO,也叫Yoast WordPress SEO。不过这货在XML站点地图老是报错!

经过了很多曲折,最后终于解决WordPress SEO插件XML站点地图404报错的问题。步骤如下

1、进入网站的后台管理,我用的是cPanel虚拟主机管理系统,其它管理系统不知道。打开资源管理器-WWW目录,找到.htaccess文件,在线编辑。线下编辑了也可以,不过还要上传。也可以WordPress后台的SEO设置-编辑文件中修改.htaccess


添加如下代码

# WordPress SEO - XML Sitemap Rewrite Fix

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

保存退出

2、找到WWW-/wp-content/plugins/wordpress-seo/inc/class-sitemaps.php编辑
把如下代码换成

function init() {
$GLOBALS['wp']->add_query_var( 'sitemap' );
$GLOBALS['wp']->add_query_var( 'sitemap_n' );

$options = get_wpseo_options();
$this->max_entries = ( isset( $options['entries-per-page'] ) && $options['entries-per-page'] != '' ) ? intval( $options['entries-per-page'] ) : 1000;

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' );
}

替换成如下代码

/**
* 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();
}

现在再看看就好了

吐槽一下:这货怎么不更新修复一下这个BUG?!

代码源于WordPress官方论坛

本博客文章如未特别说明,皆默认采用署名-相同方式共享 4.0 国际协议

相关文章

发表评论

您的电子邮箱地址不会被公开。