0

I've changed my permalinks to Custom: /%category%/%postname%/, and that (I suppose) caused my category archive pages to have two URLs. For example, category SPORT is available on /sport and /category/sport. I would like to keep only one. For some reason, pagination on /sport does not work. How do I fix this?

When I use one of the default permalinks options, it's ok and only /category/sport is available.

I've disabled all the plugins and activated the default theme, and still get this issue.

0

1 Answer 1

1

For example, category SPORT is available on /sport and /category/sport. I would like to keep only one.

I tried to reproduce that issue and I could confirm it's true, at least in WordPress v6.0.1 with the default setup, where WordPress is adding a rewrite rule (which is put at the very bottom in the rewrite rules array/list) with (.+?)/?$ being the regex (regular expression pattern) and index.php?category_name=$1 being the query.

And it's probably a core bug, or limitation in the WordPress rewriting system, but for now, you can fix the issue using the rewrite_rules_array filter like so:

add_filter( 'rewrite_rules_array', 'wpse_408488' );
function wpse_408488( $rules ) {
    $permalink_structure = get_option( 'permalink_structure' );
    if ( '%category%/%postname%' === trim( $permalink_structure, '/' ) ) {
        unset( $rules['(.+?)/?$'] ); // remove the conflicting rewrite rule
    }

    return $rules;
}

So that code will remove the additional rewrite rule (/sport), which means the category archive would only be accessible at /category/<slug>, e.g. example.com/category/sport.

And you can add that to your theme functions file, and remember to flush the rewrite rules — just visit the Permalink Settings admin page without having to do anything.

2
  • 1
    This works! Thanks! I've discovered in the meantime that using Yoast SEO (as I already do) can leave you with only /sport, if you prefer it that way. You can go to Yoast SEO / Search Appearance / Taxonomies / Category URLs (at the bottom) and set Remove the categories prefix? to Remove. Commented Aug 11, 2022 at 9:56
  • 1
    I'm glad my answer helped, and I don't use Yoast SEO, but thanks for sharing that info :) Cheers!
    – Sally CJ
    Commented Aug 11, 2022 at 13:49

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.