• Resolved glisky

    (@glisky)


    Hi!
    For technical reasons my site can be accessed from two different domains (1.com and 2.com). But to avoid duplicate content, and to show Google which adress to rank, I want the canonical to point to one of them. I know I can add this manually on each page and post with yoast. But how can I do this for the whole site, so even new pages and posts get the right canonical automatically? I cannot use the Site Address (URL) in the wordpress settings since that is the one I do not want as the canonical.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Maybellyne

    (@maybellyne)

    Hello,

    Thanks for reaching out.

    While Yoast SEO does not have an inbuilt feature on its setting for this, we suggest doing it via the available filter. You can filter the output of the canonical programmatically. To do this, you can use the wpseo_canonical filter. For more info, you can refer to this and this article.

    Thread Starter glisky

    (@glisky)

    Awesome, thank you so much! It works fine. If I want to make sure this filter is not overwritten when I update the theme, I should put this in a child theme right?

    And how should the code look if I want this for pages as well, when the code for posts look like this

    .. if ( is_singular( ‘post’ ) ) {
    return ‘https://example.com/’ . $post->post_name; …

    Plugin Support Md Mazedul Islam Khan

    (@mazedulislamkhan)

    You shall use all the modifications on a child theme to make sure that all the customization remains on the website as expected after the theme update.

    On the other hand, depending on your needs, you can apply the relevant code to only posts, pages, or anywhere you’d like. Please, refer to the WordPress official documentation about the conditional tags to understand the code better.

    We hope this helps!

    Plugin Support devnihil

    (@devnihil)

    We are going ahead and marking this issue as resolved due to inactivity. If you require any further assistance please create a new issue.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘rel canonical’ is closed to new replies.