• Resolved rkp

    (@melonmedia)


    Hi there.

    Yoast seems to generate incorrect rel=”canonical” for sub-pages that are having characters like a “+” in the URL.

    For example, the URL generated might look like as follows initially: https://www.[DOMAIN].com/keyword1+keyword2

    Yoast generates the following link tags:

    <link rel=”canonical” href=”https://www.[DOMAIN].com/keyword1%20keyword2&#8243; />

    In fact this page does not exist and therefore it creates a 404 error for the search engine. Once I deactivate Yoast entirely the canonical tag is gone which indicates to me that Yoast is producing this side-effect.

    Can you guys help with that? Help is very appreciated.
    Thx!

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support amboutwe

    (@amboutwe)

    I was unable to create a post in my local test environment that contains a + in the permalink. This likely means you are using a plugin or custom code to create the permalink.

    That said, I was able to set the canonical of a page to include the +. You or your developer can use the wpseo_canonical filter to customize the canonical for content that contains a +.

    An example snippet of how to use the filter can be found here:
    https://gist.github.com/amboutwe/2aa7dcc9a38986e11fac68c7306cc091

    Typically code snippets are added to your theme’s functions.php file. If you’re not familiar with using code snippets, these articles have wonderful advice:
    https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/
    https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

    Thread Starter rkp

    (@melonmedia)

    Hey @amboutwe Thanks for your quick reply. I tried it with following add_filter function. The preg_replace statement isolated works pretty fine, but in the functions.php it does not change anything in the output ??

    add_filter( ‘wpseo_canonical’, function( $canonical ) {
    $canonical = preg_replace(‘/%20/’, ‘+’, trailingslashit( $canonical ) );
    return $canonical;
    });

    Any ideas from your end? Thanks in advance!

    • This reply was modified 4 years, 11 months ago by rkp.
    Plugin Support devnihil

    (@devnihil)

    Unfortunately we can’t offer support on custom code. Since we don’t want to take the risk that we break your website by suggesting incorrect or incomplete code, we cannot advise you on how to make such changes. Maybe someone watching these forums can assist you further, but if your topic is inactive for 7 days, we’ll mark it as resolved to keep the overview.

    Thank you for your understanding.

    Thread Starter rkp

    (@melonmedia)

    Hey @devnihil and @amboutwe
    Thanks for your replies and yes I can see that you don’t want to take the risk for breaking any website by suggesting wrong things. But I doubt that your proposal with the wpseo_canonical filter is the right one. Since I checked the content of $canonical variable before preg_replace and it returns the url with a “+” sign and not “%20”. So preg_replace can not do it’s job, because it’ll never find pattern “%20”. Are there any other filters I can use instead of wpseo_canonical that can be called at a later point of time during run-time?

    Plugin Support amboutwe

    (@amboutwe)

    In a test environment, I was able to use the canonical filter to add a ‘+’ sign. As you were not, I suspect something else is interacting with the canonical as well. This means it’ll be trial and error to get the right timing of when to use the filter to fix the %20 issue you described.

    You or your developer can modify the code to fit your needs. Since we don’t want to take the risk that we break your website by suggesting incorrect or incomplete code, we cannot advise you on how to make such changes.

    We recommend checking out Code Poet, WordPress Jobs, or WordPress Professional Network for development & consultancy.

    Plugin Support amboutwe

    (@amboutwe)

    This thread has been marked as resolved due to a lack of activity.

    You’re always welcome to re-open this topic. Please read this post before opening a new request.

    Thanks for understanding!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Canonical creates 404 error’ is closed to new replies.