WP从 /%postname%.html重定向到 /%类别% /%postname% /



我从" /%postname%.html "更改了WP永久链接,而不是更常见的" /%Postname%/"到当前" /%类别%/%postname%/"。WP可以很好地处理这一点。但是我在将旧URL重定向到新的结构时遇到了问题。我尝试了几个重定向插件,但是当旧结构是最常见的" /%postname%/"。

时,它们似乎只是重定向

当我尝试在浏览器中打开旧URL时,我会遇到404个错误URL来自/xxx.html to /xxx/。我想解决的问题是,尽管一切正常,但我总共有2个重定向,以便进入最终并纠正"/%类别%/%postname%/"。PagesPeed Insights显示了我应该解决的登录页面重定向问题。有没有办法在.htaccess中纠正此问题?谢谢。

暂时不使用nginx重定向,我使用此

add_filter( '404_template', 'custom_redirect_to_category' );
function custom_redirect_to_category($template) {
    if ( ! is_404() ){
        return $template;
    }
    global $wp_rewrite;
    global $wp_query;
    if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure ){
        return $template;
    }   
    if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) ){
        return $template;   
    }
    $permalink = get_permalink( $post->ID );
    wp_redirect( $permalink, 301 );
    exit;
}

应该使用此插件

完成

https://wordpress.org/plugins/seo-redirection/

最新更新