I have a permalink custom structure that looks like this /blog/%category%/%postname%/

Thing is, that would make that if a post has category-1 and category-2 assigned, both /blog/category-1/post/ and /blog/category-2/post will answer.

That causes a bug where for the canonical urls, it will always answer /blog/category-1/post/ thus creating No self-referencing hreflang errors in semrush.

Is there a way to stop that behavior or redirect to always the right URL?

  • redirecting to the canonical URL is already the default behaviour in WordPress out of the box, it's likely that a plugin or custom code has broken or interfered with this or disabled it, does this site have filters that modify the canonical URLs or permalinks of pages? Anything that unhooks or modifies the redirect_canonical function that was added to the template_redirect action?
    – Tom J Nowell
    Commented Oct 3, 2023 at 14:05
  • Note this is under the assumption that a post has a single canonical URL, so cat2 will always redirect to cat1. I realise there's a second interpretation where you do not want that to happen and are asking how to disable that, as well as another interpretation where you're ok with it but don't want a canonical URL tag when visiting cat 1. Please edit the question to clarify which interpretation if any are correct so that the question is clearer
    – Tom J Nowell
    Commented Oct 3, 2023 at 14:08

1 Answer 1


Here's how I did to fix it. Added this to my functions.php

    $url = 'https://'.$url;
    $url = 'http://'.$url;
$id =  url_to_postid( $url );
$url = parse_url($url,PHP_URL_PATH);
$canonical = parse_url(wp_get_canonical_url($id),PHP_URL_PATH);
if($url != $canonical){

Basically if I hit an url that is not its own canonical, force a redirect.

@Tom J Nowell, weird but noted. Its an old website with a bunch of plugins I don't know in detail, so I guess one of them is the culprit. Just didn't take the time to investigate that part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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