Please I recently changed my site permalink structure, from https://example.com/postname/ to https://example.com/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?

  • 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 Commented May 28, 2019 at 13:11
  • Give this a shot: wordpress.org/plugins/redirection 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

1 Answer 1


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


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

enter image description here

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.


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


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

  • I did exactly this, but my problem is to make a 301 redirect from the old permalink structure /%postname%/ to the new one /%category%/%postname%/ Commented May 29, 2019 at 3:52
  • I have updated my answer with the possible solution. Commented May 30, 2019 at 21:48
  • worked like charm, thanks alot Commented May 31, 2019 at 4:12
  • Glad It helped (Y) Commented May 31, 2019 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.