Setting canonical url for sub-category without parant category tree

#377716
  • Resolved Nicohood
    Rank Math free

    I have a simple nested category setup:

    * Weather
    * Sun
    * Raining
    * Snow

    Now wordpress sets the canonical url of the Sun category to /category/weather/sun. This is bad for me, as when I later rearrange the categories to a flat hierarchy or different parent, all links will be broken. Also I dont’ like to fix this mess using redirects and I want to urls to look nice. Better would be just /category/sun

    Is it possible to do that in rankmath? If not, I’d like to propose that as a new feature. It would be good to have a category url to default to its slug, **without** its parent.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math support, and sorry for any inconvenience that might have been caused due to that.

    Rank Math automatically provides the proper canonical value once you remove or edit a category’s parent. Please refer to to my video screencast here: https://i.rankmath.com/MAHKgx

    Can you please let us know if this isn’t the case from your end?

    As for its slug, we only strip the category base and you would need to change your permalink structure. Unfortunately, we don’t have this feature yet.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Could you please add this as a feature request? This would save us lots of broken category links if the nested category was moved.

    Hello,

    Our developers are constantly improving our plugin and we valued your input. Thank you so much for sharing it.

    For now, you can create a redirection rule using our redirection module. https://rankmath.com/kb/setting-up-redirections/#redirections

    Please do not hesitate to let us know if you need our assistance with anything else

    For everyone that came here by google, here is the trick I used:

    `
    // Modify the category archive permalink
    function strip_parent_category_slug( $permalink, $term, $taxonomy ){
    // Check if it is a category (tags would be named post_tag)
    if( $taxonomy !== ‘category’ ) {
    return $permalink;
    }

    // Get the category permalink name
    // NOTE: We cannot use the get_option() default value,
    // as category_base containing an empty string would not be recognized.
    $category_base = get_option(‘category_base’);
    if ( ! $category_base ){
    $category_base = ‘category’;
    }

    return untrailingslashit( home_url(‘/’ . $category_base . ‘/’ . $term->slug ) );
    }
    // https://developer.wordpress.org/reference/hooks/term_link/
    add_filter( ‘term_link’, ‘strip_parent_category_slug’, 10, 3 );
    `

    Now you only need to add a rankmath redirect using regex:
    ^category\/.+\/(.+)$ -> https://example.com/category/$1

    It would be easy to integrate into rankmath, I’d value to have this as an official option “Strip parent category from archive url” or so.

    Hello,

    I am glad that you have made a workaround for your issue.

    I have shared your workaround with our developers. We thank you so much for your input.

    Please do not hesitate to let us know if you need our assistance with anything else.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘Setting canonical url for sub-category without parant category tree’ is closed to new replies.