Search code examples
overridingseotyposcripttypo3-9.xcanonical-link

TYPO3 seo make canonical tag slide trough rootline


Using older versions of EXT:yoast I was used that once the canonical tag was defined, it was used troughout the website from that point on (if no specific new canonical was introduced) ...

Now that this feature is dealt with by the core extension SEO, this is missing and I need to fill out every single page if the whole site needs to be indicated as duplicate for another site.

If a 'content slide trough rootline' disturbes something there could be a boolean variable introduced that sets this behaviour only if desired ...

Anybody knows how to override the core behaviour and add this for TYPO3 v9 and up ?


Solution

  • First a question: if the whole site is a duplicate of another website, wouldn't it be more logical to just set the website to "noindex,nofollow" within the robots metatag?

    If you want to override the default canonical I think the easiest way would be to XCLASS the CanonicalGenerator and the method checkDefaultCanonical.

    Basic example (which should work with 9, 10 and 11), within your site extension's ext_localconf.php:

    $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Seo\Canonical\CanonicalGenerator::class] = [
        'className' => \Vendor\Ext\Xclass\CanonicalGenerator::class
    ];
    

    And then (basic example of) the class:

    <?php
    
    namespace Vendor\Ext\Xclass;
    
    class CanonicalGenerator extends \TYPO3\CMS\Seo\Canonical\CanonicalGenerator
    {
        protected function checkDefaultCanonical(): string
        {
            $canonical = parent::checkDefaultCanonical();
            return str_replace('yourdomain.ext', 'desireddomain.ext', $canonical);
        }
    }
    

    This is of course a basic example without any configuration possibilities, but this would still leave you the possibility to manually set a canonical on a page, this method is only executed when there's no other canonical available.