帮助定制重写规则

时间:2022-01-17 作者:Steve

我继承了一个使用代码片段插件的站点,该插件有一个用于重写URL的代码片段。

不幸的是,我不是一个开发人员,所以我不确定这些是做什么的。

我们的问题是,在Yoast SEO网站地图中,博客帖子URL没有尾部斜杠。如果单击其中一个链接,它会将您重定向到带有尾部斜杠的URL。

A参考文献。com报告称,在站点地图中设置301个重定向不是一个好主意。

如果我们禁用代码片段插件,问题就会消失。我很确定这段话的意思是:

function mandoe_blog_rewrite_rules( $wp_rewrite ) {
  $new_rules = array(
    \'(([^/]+/)*blog)/page/?([0-9]{1,})/?$\' => \'index.php?pagename=$matches[1]&paged=$matches[3]\',
    \'blog/([^/]+)/?$\' => \'index.php?post_type=post&name=$matches[1]\',
    \'blog/[^/]+/attachment/([^/]+)/?$\' => \'index.php?post_type=post&attachment=$matches[1]\',
    \'blog/[^/]+/attachment/([^/]+)/trackback/?$\' => \'index.php?post_type=post&attachment=$matches[1]&tb=1\',
    \'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
    \'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
    \'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]\',     
    \'blog/[^/]+/attachment/([^/]+)/embed/?$\' => \'index.php?post_type=post&attachment=$matches[1]&embed=true\',
    \'blog/[^/]+/embed/([^/]+)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&embed=true\',
    \'blog/([^/]+)/embed/?$\' => \'index.php?post_type=post&name=$matches[1]&embed=true\',
    \'blog/[^/]+/([^/]+)/embed/?$\' => \'index.php?post_type=post&attachment=$matches[1]&embed=true\',
    \'blog/([^/]+)/trackback/?$\' => \'index.php?post_type=post&name=$matches[1]&tb=1\',
    \'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&name=$matches[1]&feed=$matches[2]\',
    \'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&name=$matches[1]&feed=$matches[2]\',
    \'blog/page/([0-9]{1,})/?$\' => \'index.php?post_type=post&paged=$matches[1]\',
    \'blog/[^/]+/page/?([0-9]{1,})/?$\' => \'index.php?post_type=post&name=$matches[1]&paged=$matches[2]\',
    \'blog/([^/]+)/page/?([0-9]{1,})/?$\' => \'index.php?post_type=post&name=$matches[1]&paged=$matches[2]\',
    \'blog/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?post_type=post&name=$matches[1]&cpage=$matches[2]\',
    \'blog/([^/]+)(/[0-9]+)?/?$\' => \'index.php?post_type=post&name=$matches[1]&page=$matches[2]\',
//    \'blog/[^/]+/([^/]+)/?$\' => \'index.php?post_type=post&attachment=$matches[1]\',
    \'blog/[^/]+/([^/]+)/trackback/?$\' => \'index.php?post_type=post&attachment=$matches[1]&tb=1\',
    \'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
    \'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$\' => \'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]\',
    \'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$\' => \'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]\',
  );
  $wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules);
}
add_action( \'generate_rewrite_rules\', \'mandoe_blog_rewrite_rules\' );

function mandoe_update_post_link( $post_link, $id = 0 ) {
  $post = get_post( $id );
  if( is_object( $post ) && $post->post_type == \'post\' ) {
    return home_url( \'/blog/\' . $post->post_name );
  }
  return $post_link;
}
add_filter( \'post_link\', \'mandoe_update_post_link\', 1, 3 );
您能否帮助确定需要调整代码的哪一部分,以便在博客帖子中添加尾部斜杠?

感谢您的帮助。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

修改博客文章URL/永久链接的部分位于mandoe_update_post_link() 函数(已连接post_link), 您需要调整的部分是:

return home_url( \'/blog/\' . $post->post_name );
只需更改\'/blog/\' . $post->post_name\'/blog/\' . $post->post_name . \'/\' URL将以斜杠结尾。但是,您可能需要清除站点(和浏览器)缓存,以便站点地图使用新的URL格式,或查看应用的更改。

相关推荐