categories - Redirect Changed Permalink on wordpress

Please I recently changed my site permalink structure, fromto I have searched online and even here but all I am seei

Please I recently changed my site permalink structure, from / to /

I have searched online and even here but all I am seeing is /postdate/year/postname/ to /postname/

Since I have many categories I cannot make an htaccess 301 redirection for this, please is there any other way this can be done?

Please I recently changed my site permalink structure, from https://example/postname/ to https://example/category/postname/

I have searched online and even here but all I am seeing is /postdate/year/postname/ to /postname/

Since I have many categories I cannot make an htaccess 301 redirection for this, please is there any other way this can be done?

Share Improve this question asked May 28, 2019 at 12:34 Godwin Alex OgbondaGodwin Alex Ogbonda 1192 silver badges13 bronze badges 4
  • Are you a programmer? If so, yes this is possible. But you will need to invest some time to figure it out. If not, you should hire someone. – kero Commented May 28, 2019 at 13:06
  • I dont know much on wordpress programming. Please kindly help out – Godwin Alex Ogbonda Commented May 28, 2019 at 13:11
  • Give this a shot: wordpress/plugins/redirection – MikeNGarrett Commented May 28, 2019 at 13:36
  • 1 @GodwinAlexOgbonda Usually you hook into template_redirect action for redirects. In your case it could be summarized as: check if the URL has a post slug, if that post slug exists in the db, redirect to the new permalink – kero Commented May 28, 2019 at 14:04
Add a comment  | 

1 Answer 1

Reset to default 1

I assume you have added new permalink structure by defining Custom Permalink Structure on

https://yoursite/wp-admin/options-permalink.php

and added /%category%/%postname%/ in the custom structure field. This is the WordPress default/suggested method.

After this, you need to flush the old permalink structure by saving and reloading the permalink settings page.

This will not work for custom post type and only works for WordPress default post type. For custom post types you would have to apply additional hooks or defile it while registering custom post type function.

You should read the WordPress official documentation on using permalinks.

Update:

In this case, you can use "404_template" filter.

Example:

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;

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745449567a4628206.html

相关推荐

  • categories - Redirect Changed Permalink on wordpress

    Please I recently changed my site permalink structure, fromto I have searched online and even here but all I am seei

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信