3

I have a website that contains 200 categories with thousands of words in on every category page. Most of these category pages is where my traffic comes from, and have the following URL structure: example.com/tennis-shoes/.

However, now I want to add parent categories in order to organize the content for my visitors, which means that the URL:s will be example.com/parent-category-name/tennis-shoes/. This will obviously have a major negative impact on my rankings and it's therefor something I don't want to risk.

I've done some research and testing and realized that I have 2 problems with this addition of parent categories, namely the website URLs and the sitemap URLs.

The sitemap URLs I've managed to change with the following code:

function sitemap_post_url( $url, $term, $object ) {
    // Remove the parent category slug from the URLs by replacing it with an empty string
    if (strpos($url, '/tennis-shoes/') !== false) {
        return str_replace("/tennis-shoes/", "", $url);
    }

    return $url;
}
add_filter( 'wpseo_sitemap_entry', 'sitemap_post_url', 10, 3 );

As for the website URLs I don't know what to do, maybe I need to add some kind of redirect hook/filter in functions.php - similar to the filter code above - so that the URL:s to my now child categories (since I've added parents) exclude the parent slug from the URL so it does not affect my current indexed URLs.

In short what I want to do is to organize my site with parent categories but keep all my current URLs so it doesn't affect me SEO.

Hence, my questions are:

  • How can I exclude the parent category slug for the website URLs, just like I do for the sitemap URLs with the filter code?
  • If I manage to solve the above, will my SEO to my currently indexed categories (and 90% of my search traffic) be affected in a negative way by adding these parent categories in order to improve the structure for my visitors?

1 Answer 1

1

Below is a pretty standard way to strip the category base from URLs via functions.php. It is similar to your sitemap filter.

function no_cats($string, $type) {
    if ($type != "single" && $type == "category" && strpos($string, "category") !== false) {
        $no_cats = str_replace("/category/", "/", $string);
        return trailingslashit($no_cats);
    }
    return $string;
}
add_filter("user_trailingslashit", "no_cats", 100, 2);

However, this to me seems unnecessary considering that you want to organize your site with parent categories. Just 301 redirect your existing pages, your current URLs will be fine.

1
  • Hi Mike. Isn't your code just removing the category base and not the parent category from the website urls? Jun 28, 2022 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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