admin管理员组

文章数量:1122832

This client is using Yoast SEO (the free version) to specify primary categories for posts, a feature not available in the WordPress core. They discovered recently, via an SEO report, that the same post can be accessed multiple ways by changing the category slug in the URL. The WordPress permalink settings are set to a custom structure, as follows: /company/news-views/%category%/%postname%/ So, while the canonical URL of a post might be /company/news-views/sustainability/blah-blah-blah/, where sustainability is the primary category, you can also access the same post via /company/news-views/aircraft-management/blah-blah-blah/, where aircraft-management is another category that has been set on the post. I've checked the source HTML of both URLs and both have the same canonical URL meta tag. That is, the HTML looks something like:

<link rel="canonical" href="/" class="yoast-seo-meta-tag" />

My question is, how can we force an automatic redirect to the canonical URL if a non-canonical URL is entered? One would think that Yoast would have this feature. (Maybe it's only available in the premium version?) I'm aware of the redirect_canonical filter, but I'm not sure how to obtain the canonical URL by using that filter, especially since the filter does not supply a $post or $post_id parameter. The redirect_canonical function fires before the global $post variable is set. Or perhaps I need to override a different filter/action, like maybe pre_get_posts?

This client is using Yoast SEO (the free version) to specify primary categories for posts, a feature not available in the WordPress core. They discovered recently, via an SEO report, that the same post can be accessed multiple ways by changing the category slug in the URL. The WordPress permalink settings are set to a custom structure, as follows: /company/news-views/%category%/%postname%/ So, while the canonical URL of a post might be /company/news-views/sustainability/blah-blah-blah/, where sustainability is the primary category, you can also access the same post via /company/news-views/aircraft-management/blah-blah-blah/, where aircraft-management is another category that has been set on the post. I've checked the source HTML of both URLs and both have the same canonical URL meta tag. That is, the HTML looks something like:

<link rel="canonical" href="https://www.example.com/company/news-views/sustainability/blah-blah-blah/" class="yoast-seo-meta-tag" />

My question is, how can we force an automatic redirect to the canonical URL if a non-canonical URL is entered? One would think that Yoast would have this feature. (Maybe it's only available in the premium version?) I'm aware of the redirect_canonical filter, but I'm not sure how to obtain the canonical URL by using that filter, especially since the filter does not supply a $post or $post_id parameter. The redirect_canonical function fires before the global $post variable is set. Or perhaps I need to override a different filter/action, like maybe pre_get_posts?

Share Improve this question asked Mar 29, 2024 at 19:54 William BeaumontWilliam Beaumont 1233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I had the exact same problem, and I believe that was probably caused because Wordpress doesn't know about the primary category (that is something added by Yoast SEO instead).

So is something that in my opinion Yoast SEO should manage, hopefully in next releases they'll do it.

In the meanwhile it looks I've smoothly resolved adding even Permalink Manager (free version is ok) plugin.

It solves both these two problems

  • It adds redirects to canonical even when the permalink includes the category
  • It makes internal post and pages links point correctly to the path composed by the primary category

Could even be even just random, and maybe just a cache issue that installing Permalink Manager solved, but I've tried to deactivate it and I had again the same issue. So it looks that that plugin is the solution.

Please let me know if it solves the problem even in your case.

本文标签: How can I do automatic redirects to canonical URLs as defined by Yoast SEO