Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve SEF canonical issues #9565

Merged
merged 9 commits into from Mar 26, 2016
36 changes: 29 additions & 7 deletions plugins/system/sef/sef.php
Expand Up @@ -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');
}

/**
Expand Down
1 change: 1 addition & 0 deletions plugins/system/sef/sef.xml
Expand Up @@ -22,6 +22,7 @@
<field name="domain" type="url"
description="PLG_SEF_DOMAIN_DESCRIPTION"
label="PLG_SEF_DOMAIN_LABEL"
hint="https://www.example.com"
filter="url"
validate="url"
/>
Expand Down