WordPress posts in multiple categories - how to add automatic redirects

PUBLISHED 13 Aug 2023 - UPDATED 15 Aug 2023

Adding WordPress posts to more than one category can cause duplicate content issues. Here's how to fix this with automatic redirects using an SEO plugin such as Rank Math and a WordPress function.

Adding a post to multiple categories

If you put a WordPress post in more than one category and your custom permalink structure includes the category name, then your post will be available at more than one URL.

Let me give you an example from this site. My custom permalink structure (as set in the WordPress dashboard, Settings > Permalinks) is: /%category%/%postname%/

Custom permalink structure in WordPress

My custom permalink structure includes the /%category%/ tag

I created a post and added it both my 'Analytics' and 'SEO' categories. This meant it was available at two different URLs (permalinks):

This is, by definition, duplicate content - the same content available in more than one location. This can cause SEO issues, for example you might want Google to index one version but it chooses to index a different one. Also, the benefit of any inbound links is diluted by being spread across multiple URLs.

Adding canonical tags

Many of the SEO issues caused by placing a post in multiple categories can be mitigated by using a WordPress SEO plugin such as Rank Math or Yoast SEO (the free version of either will do). These plugins allow you to select one of your chosen categories as the 'primary category'.

The exact mechanism for doing this differs slightly depending on how you create your WordPress posts. For example, if you use the Classic Editor, Rank Math adds a radio button to the Categories section of the 'Edit Post' page:

WordPress categories with one chosen as 'primary category'

If you prefer to use the newer Block Editor, Rank Math adds a dropdown called 'SELECT PRIMARY TERM' instead:

Select Primary Term in WordPress Block Editor

However you do it, selecting a primary category adds a so-called canonical tag to each of the pages indicating which is the main or 'canonical' one. (Technically speaking, it's a <link> element with the attribute rel="canonical", as you'll see in the next screengrab.)

As well as telling Google which version it should index, the canonical tag helps to consolidate ranking signals - in other words, the canonical version will get the benefit of inbound links to the other versions.

Continuing with my example, I used Rank Math to specify 'SEO' as the primary category for my post. This added the following canonical tag to both versions:

<link rel="canonical" href="https://www.technicallyproduct.co.uk/seo/spike-in-direct-traffic-it-could-be-google-discover/" />

You can see this by going to the post and viewing the page source (right-click and 'View page source', or Ctrl+U in Chrome):

Canonical tag added by Rank Math

Click to view a larger version

Bear in mind that Google takes a canonical tag as a "strong signal", but may choose to ignore it if it has reasons to do so. (For example, Google prefers to canonicalize HTTPS pages over equivalent HTTP pages.)

Using redirects

Another way to mitigate the problem of duplicate content caused by placing WordPress posts in multiple categories is to use redirects. In other words, you redirect all the non-canonical URLs to your one, chosen, canonical version. Google states that this is also a "strong signal" and actually stacks with the canonical tag:

"Keep in mind that these methods can stack and thus become more effective when combined. This means that when you use two or more of the methods, that will increase the chance of your preferred canonical URL appearing in search results."

Using redirects has other benefits, for example all traffic will show up under a single URL in your analytics tool such as Google Analytics 4 (GA4).

Automating redirects

Now you could add your redirects in one at a time using a WordPress plugin such as Redirection, but this would be very time consuming should you often put posts in multiple categories. If you've used Rank Math or Yoast SEO to specify a primary category, there's a quicker way to do it.

It involves using a WordPress function created by the lovely people at Happy Prime. It's the final code snippet on the following page: https://happyprime.co/2021/10/14/using-a-primary-category-in-wp-permalinks/

You will need to make one change to the snippet, swapping '_primary_category' to either '_yoast_wpseo_primary_category' (for Yoast) or 'rank_math_primary_category' (for Rank Math). Here's what the code looks like for Rank Math, with the amended line highlighted:

If you know what you're doing, you could add the function to your site by editing your theme's functions.php file - but the easier and safer option is to use a WordPress plugin such as Code Snippets.

This lets you add the snippet and set it to 'only run on site front end' - which means it will definitely not break anything in the admin area. The function will continue to run even if you change theme, but on the other hand can easily be disabled using a toggle in the Code Snippets plugin.

Here's what it looks like in Code Snippets:

Edit snippet in Code Snippets WordPress plugin

Click to view a larger version

I put this in place on my site, and now the URL https://www.technicallyproduct.co.uk/analytics/spike-in-direct-traffic-it-could-be-google-discover/ automatically redirects to the canonical version based on my chosen primary category, https://www.technicallyproduct.co.uk/seo/spike-in-direct-traffic-it-could-be-google-discover/

The website Redirect Checker shows it is a permanent (301) redirect:

Redirect Checker showing 301 redirect

Subscribe
Notify of
guest
3 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Trisha
Trisha
3 months ago

Very helpful, thank you. Can you tell me if adding the redirection code snippet (not a problem for me as I maintain a custom plugin for such things) is necessary if we've been using RankMath for a long time (longer than most of the content we've posted for the last 2-3 years)? I understand the benefit of the redirections and can see where older content might need canonicals set, but wondering if it's needed for anything posted since setting up RankMath (a great plugin, by the way - I've used both Yoast and AIO in the past, and find RM… Read more »

Trisha
Trisha
3 months ago
Reply to  James Clark

Thank you! I'll pay closer attention to what my analytics are telling me, but good to know it may not be necessary.

James Clark
Hi! I'm James Clark and I'm a freelance web analyst from the UK. I'm here to help with your analytics, ad operations, and SEO issues.
3
0
What do you think? Leave a commentx
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram