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.