I have changed the category from

"file-recovery" -> "zip-file-recovery"


"file-recovery" -> "archive-and-backup-file-recovery" -> "zip-file-recovery"

However, after that, I find for the same post, there are two kinds of URLs that can access it. For example:

Old structure: https://www.datanumen.com/blogs/category/data-recovery/file-recovery/zip-file-recovery/page/3/

New structure: https://www.datanumen.com/blogs/category/data-recovery/file-recovery/archive-and-backup-file-recovery/zip-file-recovery/page/3/

Both pages will set the new structure URL as the canonical URL. But when visiting the old structure URL, it will NOT be redirected to the new one.

Is this designed by purpose? And if that is the case, if I change the category for 10 times, then there will 11 copies of the the same post?

  • @SallyCJ, the current permalink structure is post name, i.e., datanumen.com/blogs/sample-post
    – alancc
    Commented Dec 16, 2021 at 3:07
  • Thanks, but please ignore my previous comment (which I've deleted), and I hope my answer helps?
    – Sally CJ
    Commented Dec 16, 2021 at 17:53

1 Answer 1


when visiting the old structure URL, it will NOT be redirected to the new one

That's because:

  1. The default category taxonomy is hierarchical and so does the permalink structure, hence its rewrite rules use a RegEx pattern like /category/(.+?)/? which will match any category path even if it contains invalid/non-existent term slugs. ( That (.+?) part is the category path )

  2. And as long as the last term slug in the category path (in the above rule) is valid, i.e. the term exists, then WordPress would load the category page normally, i.e. without throwing a 404 error or redirecting to the correct URL (the one with the right category path).

So as for the first URL in question, where the category path is data-recovery/file-recovery/zip-file-recovery, it contains valid term slugs, and thus WordPress didn't redirect to the new URL.

And as you may have guessed it now, invalid category paths like foo/bar/zip-file-recovery, where "foo" and "bar" are non-existent categories, would also work because the last slug is valid.

How to fix the issue

You can use this to ensure the category path is valid before loading the category archive page:

add_action( 'wp', 'wpse_399267_wp', 1 );
function wpse_399267_wp( $wp ) {
    if ( is_category() &&
        ( $the_cat = get_queried_object() ) &&
        ! empty( $wp->query_vars['category_name'] )
    ) {
        // Check if the category path is valid.
        $cat = get_category_by_path( $wp->query_vars['category_name'] );

        // If the category path is invalid, redirect to the correct one.
        if ( empty( $cat ) && ! is_wp_error( $cat ) ) {
            wp_redirect( get_category_link( $the_cat ) );

Or if you'd rather want to show a 404 error page, then..

// Replace these:
wp_redirect( get_category_link( $the_cat ) );

// with these:
status_header( 404 );
  • Thank you very much. I make a test and find this is really the case. I try to define a redirection from ^/blogs/category/data-recovery/file-recovery/zip-file-recovery(.*) to /blogs/category/data-recovery/file-recovery/archive-and-backup-file-recovery/zip-file-recovery$1 in Redirection plugin. But that does not work. Why?
    – alancc
    Commented Dec 20, 2021 at 3:17
  • Did you check the option labeled "Regex" when you created the redirection? But please check the documentation for more details on using the plugin. And if answer answered your question or helped with the issue in question, I'd appreciate it if you can accept or upvote my answer.
    – Sally CJ
    Commented Dec 20, 2021 at 9:19
  • Yes, I enable the "Regex" option but that does not work.
    – alancc
    Commented Dec 21, 2021 at 2:19
  • Then I guess there's a plugin or custom code (in your theme) which is causing the issue. Try switching to a default theme (e.g. 2021) and see if the redirection now works. If not, try deactivating all plugins except the Redirection plugin, and see if the same issue persists.
    – Sally CJ
    Commented Dec 21, 2021 at 10:59
  • 1
    Thank you so much. I will try.
    – alancc
    Commented Dec 22, 2021 at 7:52

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.