Support » Fixing WordPress » Correcting Canonical URL

  • Resolved christopheran



    I’ve moved my wordpress site to a new domain.

    In the old domain, I had permalinks set up with a number at the end of the post name, example: (some extra digits and the post id number).

    With my new domain, I don’t need the number, so I set my permalinks up with just /post-title.

    Then set up a 301 redirect from the old to the new domain, and WP seems to successfully redirect the numbered to the numbered url.

    However, sometimes the number is left on the end of the URL, and the canonical url in the source code includes the number.

    So I think I need to add something in fuctions.php to adjust the url, strip out the appended numbers, and return the canonical url without them.

    Not sure how to write the function though… can anyone help? The numbers at the end that need to be removed always start with 367, if that helps.



Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve tried putting this (adapted from a script found online) in my functions php, to make the canonical url identical to the permalink, but it doesn’t seem to have any affect –

    add_action( 'wp_head', 'fix_canonical', 11 ); 
    function fix_canonical()
        // Remove
        remove_action( 'wp_head', 'rel_canonical' );
     $true_can = get_permalink();
        // add back corrected
            '<link rel="canonical" href="%s" />', 
             esc_url( $true_can )

    Ok, managed to change this around so I 301 redirect to the correct url, so no longer need to fix the canonical 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Correcting Canonical URL’ is closed to new replies.