Modify Head rel=canonical Topic is solved

Discuss Search Engine Optimization in relation to Joomla! 3.x. This forum will also have discussions on SEF/SEO Joomla! 3.x extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Modify Head rel=canonical

Post by DaveOzric » Fri Feb 16, 2018 10:07 pm

This has bugged me for a while. Why all the publishing options in Joomla articles but no rel=canonical option.

You have:
Key Reference
Robots
Author
Content Rights
External Reference

Key reference and external reference are a mystery to me. Anyone?

What would it take to make one of these fields a rel=canonical instead?

So change the output in the head from
<meta name="xreference" content="http://google.com" />
to
<link rel="canonical" href="http://google.com">

There must be a way to change this so I can enter this in my articles and use the canonical benefit if I want.

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Modify Head rel=canonical

Post by effrit » Sat Feb 17, 2018 7:31 am

make template override for article, insert

Code: Select all

$canon = $this->document->_metaTags["name"]["xreference"];
if($canon){
	$this->document->addCustomTag("<link rel='canonical' href='{$canon}'>"); 
	unset($this->document->_metaTags["name"]["xreference"]);
}

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Sat Feb 17, 2018 3:57 pm

I've not done many overrides. Insert this anywhere or can I just change the xreference completely as I will not use it anyway?

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Modify Head rel=canonical

Post by effrit » Sat Feb 17, 2018 4:23 pm

see in template folder
\html\com_content\article\default.php

if there is no such path, go to
template manager -> Template -> Details and Files -> Create overrides ->com_content - article


open default.php and inser my code after first 10-20 strings in php code section.

save, open article and fill External Reference, check page code.

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Sat Feb 17, 2018 4:53 pm

Any chance you can be exact? This is all gibberish to me. Where would this go?

Code: Select all

<?php
/**
 * @package     Joomla.Site
 * @subpackage  com_content
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT . '/helpers');

// Create shortcuts to some parameters.
$params  = $this->item->params;
$images  = json_decode($this->item->images);
$urls    = json_decode($this->item->urls);
$canEdit = $params->get('access-edit');
$user    = JFactory::getUser();
$info    = $params->get('info_block_position', 0);

// Check if associations are implemented. If they are, define the parameter.
$assocParam = (JLanguageAssociations::isEnabled() && $params->get('show_associations'));
JHtml::_('behavior.caption');

?>
<div class="item-page<?php echo $this->pageclass_sfx; ?>" itemscope itemtype="https://schema.org/Article">
	<meta itemprop="inLanguage" content="<?php echo ($this->item->language === '*') ? JFactory::getConfig()->get('language') : $this->item->language; ?>" />
	<?php if ($this->params->get('show_page_heading')) : ?>
	<div class="page-header">
		<h1> <?php echo $this->escape($this->params->get('page_heading')); ?> </h1>
	</div>
	<?php endif;
	if (!empty($this->item->pagination) && $this->item->pagination && !$this->item->paginationposition && $this->item->paginationrelative)
	{
		echo $this->item->pagination;
	}
	?>

	<?php // Todo Not that elegant would be nice to group the params ?>
	<?php $useDefList = ($params->get('show_modify_date') || $params->get('show_publish_date') || $params->get('show_create_date')
	|| $params->get('show_hits') || $params->get('show_category') || $params->get('show_parent_category') || $params->get('show_author') || $assocParam); ?>

	<?php if (!$useDefList && $this->print) : ?>
		<div id="pop-print" class="btn hidden-print">
			<?php echo JHtml::_('icon.print_screen', $this->item, $params); ?>
		</div>
		<div class="clearfix"> </div>
	<?php endif; ?>
	<?php if ($params->get('show_title') || $params->get('show_author')) : ?>
	<div class="page-header">
		<?php if ($params->get('show_title')) : ?>
			<h2 itemprop="headline">
				<?php echo $this->escape($this->item->title); ?>
			</h2>
		<?php endif; ?>
		<?php if ($this->item->state == 0) : ?>
			<span class="label label-warning"><?php echo JText::_('JUNPUBLISHED'); ?></span>
		<?php endif; ?>
		<?php if (strtotime($this->item->publish_up) > strtotime(JFactory::getDate())) : ?>
			<span class="label label-warning"><?php echo JText::_('JNOTPUBLISHEDYET'); ?></span>
		<?php endif; ?>
		<?php if ((strtotime($this->item->publish_down) < strtotime(JFactory::getDate())) && $this->item->publish_down != JFactory::getDbo()->getNullDate()) : ?>
			<span class="label label-warning"><?php echo JText::_('JEXPIRED'); ?></span>
		<?php endif; ?>
	</div>
	<?php endif; ?>
	<?php if (!$this->print) : ?>
		<?php if ($canEdit || $params->get('show_print_icon') || $params->get('show_email_icon')) : ?>
			<?php echo JLayoutHelper::render('joomla.content.icons', array('params' => $params, 'item' => $this->item, 'print' => false)); ?>
		<?php endif; ?>
	<?php else : ?>
		<?php if ($useDefList) : ?>
			<div id="pop-print" class="btn hidden-print">
				<?php echo JHtml::_('icon.print_screen', $this->item, $params); ?>
			</div>
		<?php endif; ?>
	<?php endif; ?>

	<?php // Content is generated by content plugin event "onContentAfterTitle" ?>
	<?php echo $this->item->event->afterDisplayTitle; ?>

	<?php if ($useDefList && ($info == 0 || $info == 2)) : ?>
		<?php // Todo: for Joomla4 joomla.content.info_block.block can be changed to joomla.content.info_block ?>
		<?php echo JLayoutHelper::render('joomla.content.info_block.block', array('item' => $this->item, 'params' => $params, 'position' => 'above')); ?>
	<?php endif; ?>

	<?php if ($info == 0 && $params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) : ?>
		<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>

		<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
	<?php endif; ?>

	<?php // Content is generated by content plugin event "onContentBeforeDisplay" ?>
	<?php echo $this->item->event->beforeDisplayContent; ?>

	<?php if (isset($urls) && ((!empty($urls->urls_position) && ($urls->urls_position == '0')) || ($params->get('urls_position') == '0' && empty($urls->urls_position)))
		|| (empty($urls->urls_position) && (!$params->get('urls_position')))) : ?>
	<?php echo $this->loadTemplate('links'); ?>
	<?php endif; ?>
	<?php if ($params->get('access-view')) : ?>
	<?php echo JLayoutHelper::render('joomla.content.full_image', $this->item); ?>
	<?php
	if (!empty($this->item->pagination) && $this->item->pagination && !$this->item->paginationposition && !$this->item->paginationrelative) :
		echo $this->item->pagination;
	endif;
	?>
	<?php if (isset ($this->item->toc)) :
		echo $this->item->toc;
	endif; ?>
	<div itemprop="articleBody">
		<?php echo $this->item->text; ?>
	</div>

	<?php if ($info == 1 || $info == 2) : ?>
		<?php if ($useDefList) : ?>
				<?php // Todo: for Joomla4 joomla.content.info_block.block can be changed to joomla.content.info_block ?>
			<?php echo JLayoutHelper::render('joomla.content.info_block.block', array('item' => $this->item, 'params' => $params, 'position' => 'below')); ?>
		<?php endif; ?>
		<?php if ($params->get('show_tags', 1) && !empty($this->item->tags->itemTags)) : ?>
			<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
			<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
		<?php endif; ?>
	<?php endif; ?>

	<?php
	if (!empty($this->item->pagination) && $this->item->pagination && $this->item->paginationposition && !$this->item->paginationrelative) :
		echo $this->item->pagination;
	?>
	<?php endif; ?>
	<?php if (isset($urls) && ((!empty($urls->urls_position) && ($urls->urls_position == '1')) || ($params->get('urls_position') == '1'))) : ?>
	<?php echo $this->loadTemplate('links'); ?>
	<?php endif; ?>
	<?php // Optional teaser intro text for guests ?>
	<?php elseif ($params->get('show_noauth') == true && $user->get('guest')) : ?>
	<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
	<?php echo JHtml::_('content.prepare', $this->item->introtext); ?>
	<?php // Optional link to let them register to see the whole article. ?>
	<?php if ($params->get('show_readmore') && $this->item->fulltext != null) : ?>
	<?php $menu = JFactory::getApplication()->getMenu(); ?>
	<?php $active = $menu->getActive(); ?>
	<?php $itemId = $active->id; ?>
	<?php $link = new JUri(JRoute::_('index.php?option=com_users&view=login&Itemid=' . $itemId, false)); ?>
	<?php $link->setVar('return', base64_encode(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid, $this->item->language))); ?>
	<p class="readmore">
		<a href="<?php echo $link; ?>" class="register">
		<?php $attribs = json_decode($this->item->attribs); ?>
		<?php
		if ($attribs->alternative_readmore == null) :
			echo JText::_('COM_CONTENT_REGISTER_TO_READ_MORE');
		elseif ($readmore = $attribs->alternative_readmore) :
			echo $readmore;
			if ($params->get('show_readmore_title', 0) != 0) :
				echo JHtml::_('string.truncate', $this->item->title, $params->get('readmore_limit'));
			endif;
		elseif ($params->get('show_readmore_title', 0) == 0) :
			echo JText::sprintf('COM_CONTENT_READ_MORE_TITLE');
		else :
			echo JText::_('COM_CONTENT_READ_MORE');
			echo JHtml::_('string.truncate', $this->item->title, $params->get('readmore_limit'));
		endif; ?>
		</a>
	</p>
	<?php endif; ?>
	<?php endif; ?>
	<?php
	if (!empty($this->item->pagination) && $this->item->pagination && $this->item->paginationposition && $this->item->paginationrelative) :
		echo $this->item->pagination;
	?>
	<?php endif; ?>
	<?php // Content is generated by content plugin event "onContentAfterDisplay" ?>
	<?php echo $this->item->event->afterDisplayContent; ?>
</div>

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Modify Head rel=canonical

Post by effrit » Sat Feb 17, 2018 4:57 pm

after
$info = $params->get('info_block_position', 0);

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Sat Feb 17, 2018 5:05 pm

This works. Although MOZ shows " not ' <link rel="canonical"

Does this matter?

User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Modify Head rel=canonical

Post by effrit » Sat Feb 17, 2018 5:17 pm

i cant understand what you mean. What is MOZ?

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Sat Feb 17, 2018 5:23 pm

MOZ is the leading SEO org. https://moz.com/blog/complete-guide-to- ... nd-why-not

All the examples I found have <link rel="canonical" the output you gave me has rel='canonical'

So ' instead of " wrapping it. Not sure if it matters.

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Sat Feb 17, 2018 5:24 pm


User avatar
effrit
Joomla! Guru
Joomla! Guru
Posts: 846
Joined: Sun Nov 12, 2017 2:21 pm
Location: middle of Russia
Contact:

Re: Modify Head rel=canonical

Post by effrit » Sat Feb 17, 2018 5:47 pm

this doesn't matter.
but if it bother you, replace to

Code: Select all

$this->document->addCustomTag('<link rel="canonical" href="'.$canon.'">');

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Sat Feb 17, 2018 6:21 pm

I was not sure, thanks for letting me know.

If this is so easy why is it not in the core? Things like this can really set us apart from WP and the other CMS platforms. I do not like overrides, the few times I have used them something eventually goes awry after enough updates.

This will be helpful for me.

HelpJ
Joomla! Intern
Joomla! Intern
Posts: 80
Joined: Thu Dec 01, 2016 3:38 pm
Contact:

Re: Modify Head rel=canonical

Post by HelpJ » Wed Feb 21, 2018 6:14 pm

That I want to know to. Duplicate/canonical is one of big problems on all CMS. Joomla is failing to solve this from begining.
Joomla Pro Support https://www.help-joomla.com

kev211
Joomla! Apprentice
Joomla! Apprentice
Posts: 14
Joined: Tue Feb 27, 2018 10:20 am

Re: Modify Head rel=canonical

Post by kev211 » Thu Mar 22, 2018 7:14 am


wesdunn
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Dec 21, 2015 3:20 am
Location: Alicante
Contact:

Re: Modify Head rel=canonical

Post by wesdunn » Thu Apr 19, 2018 9:45 pm

Kev above recommends a good extension that will do the job but you can also force this behaviour with most control panels or the .htaccess file to specify your preferred version of a URL..

hubneraf
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Feb 22, 2010 7:58 pm

Re: Modify Head rel=canonical

Post by hubneraf » Tue Feb 26, 2019 7:45 am

sorry if i write on this old post but i user this override. It's all ok but i receive from my website, on article where I DON'T WRITE THE CANONICAL URL, this error

Notice: Undefined index: xreference in.../default.php (page override template)

how can i resolve this? thanks

User avatar
DaveOzric
Joomla! Ace
Joomla! Ace
Posts: 1591
Joined: Sat May 22, 2010 10:29 pm
Contact:

Re: Modify Head rel=canonical

Post by DaveOzric » Tue Mar 12, 2019 1:55 pm

No Joomla Gurus going to chime in here? Why are these useless weird fields in the article publishing area but not something useful like this?

Can't we get this in Joomla 4?

Or add a custom field option that can be used for the head tag? That would be nice too.

newlinedesign
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Mar 06, 2018 6:11 pm

Re: Modify Head rel=canonical

Post by newlinedesign » Fri Oct 11, 2019 8:25 pm

DaveOzric wrote:
Tue Mar 12, 2019 1:55 pm
No Joomla Gurus going to chime in here? Why are these useless weird fields in the article publishing area but not something useful like this?

Can't we get this in Joomla 4?

Or add a custom field option that can be used for the head tag? That would be nice too.
Dave,

This may help.
https://www.danielmorell.com/blog/how-t ... nical-tags

Adam

User avatar
kgeary007
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Mon Feb 07, 2011 5:18 pm

Re: Modify Head rel=canonical

Post by kgeary007 » Wed Jan 08, 2020 6:24 pm

This solution is exactly what I needed! We re-print content from external publishers and they are very concerned with the canonical links. So I use the external reference field in joomla for the url of the original article. We don't have issues with internal duplicate pages, but the external link to the original source has been a problem I couldn't solve until now with your excellent code.
THANKS SO MUCH!!

$canon = $this->document->_metaTags["name"]["xreference"];
if($canon){
$this->document->addCustomTag("<link rel='canonical' href='{$canon}'>");
unset($this->document->_metaTags["name"]["xreference"]);
}


Locked

Return to “Search Engine Optimization (Joomla! SEO) in Joomla! 3.x”