diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index 3fa5a85457ebb..c4f8c3c0109bb 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -40,20 +40,42 @@ public function onAfterDispatch() return; } - $uri = JUri::getInstance(); - $domain = $this->params->get('domain'); + $sefDomain = $this->params->get('domain', ''); - if ($domain === false || $domain === '') + // Don't add a canonical html tag if no alternative domain has added in SEF plugin domain field. + if (empty($sefDomain)) { - $domain = $uri->toString(array('scheme', 'host', 'port')); + return; } - $link = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); + // Check if a canonical html tag already exists (for instance, added by a component). + $canonical = ''; + foreach ($doc->_links as $linkUrl => $link) + { + if (isset($link['relation']) && $link['relation'] === 'canonical') + { + $canonical = $linkUrl; + break; + } + } - if (rawurldecode($uri->toString()) !== $link) + // If a canonical html tag already exists get the canonical and change it to use the SEF plugin domain field. + if (!empty($canonical)) { - $doc->addHeadLink(htmlspecialchars($link), 'canonical'); + // Remove current canonical link. + unset($doc->_links[$canonical]); + + // Set the current canonical link but use the SEF system plugin domain field. + $canonical = $sefDomain . JUri::getInstance($canonical)->toString(array('path', 'query', 'fragment')); + } + // If a canonical html doesn't exists already add a canonical html tag using the SEF plugin domain field. + else + { + $canonical = $sefDomain . JUri::getInstance()->toString(array('path', 'query', 'fragment')); } + + // Add the canonical link. + $doc->addHeadLink(htmlspecialchars($canonical), 'canonical'); } /** diff --git a/plugins/system/sef/sef.xml b/plugins/system/sef/sef.xml index c28b3b0afa04d..107f30dae7348 100644 --- a/plugins/system/sef/sef.xml +++ b/plugins/system/sef/sef.xml @@ -22,6 +22,7 @@