Question

I am using Joomla 2.5 and I want to change the canonical link in the header. I do this in category view (components/com_content/category/tmpl/default.php)

$url        = JURI::root();
$sch        = parse_url($url, PHP_URL_SCHEME);
$server     = parse_url($url, PHP_URL_HOST);
$canonical  = $this->escape($_SERVER['REQUEST_URI']);    
$document->addCustomTag('<link rel="canonical" href="'.$sch.'://'.$server.$canonical.'"/>');

It prints the right canonical, but it also leaves the old canonical link there so that I have 2 canonical links in the header.

How can I change or delete the old canonical link?

Was it helpful?

Solution 2

What you probably want to do instead is something like the following:

$doc_data = $document->getHeadData();
$url        = JURI::root();
$sch        = parse_url($url, PHP_URL_SCHEME);
$server     = parse_url($url, PHP_URL_HOST);
$canonical  = $this->escape($_SERVER['REQUEST_URI']); 
$newtag     = '<link rel="canonical" href="'.$sch.'://'.$server.$canonical.'"/>'

$replaced = false;
foreach ($doc_data['custom'] as $key=>$c) {
    if (strpos($c, 'rel="canonical"')!==FALSE) {
        $doc_data['custom'][$key] = $newtag;
        $replaced = true;
    }
}
if (!$replaced) {
    $doc_data['custom'][] = $newtag;
}

$document->setHeadData($doc_data);

This will grab all of the current head data from the document, including the canonical link that you want to replace. It will search through the custom set (where I'm guessing this will be) and if it finds it, replace it with yours. If it doesn't find it, then it tacks it on at the end. Just in case.

Potential problems with this that I can see right away:

  1. If the tag contained rel='canonical' with single quotes it would not be found, so you may have to adjust that.
  2. The tag may have been placed in a different section of what I've termed $doc_data. You may want to do a var_dump($doc_data}; to confirm the location of the variable in this array.

OTHER TIPS

I have found the following to work for me with Joomla! 3.2.1. You can directly modify the

$_links 

variable in the JHtmlDocument object.

I'm doing a subset of the following in a particular view of my component because the URL that Joomla! is coming up with is not correct.

Hope this helps.

    $document = JFactory::getDocument();
    foreach($document->_links as $key=> $value)
    {
        if(is_array($value))
        {
            if(array_key_exists('relation', $value))
            {
                if($value['relation'] == 'canonical')
                {                       
                    // we found the document link that contains the canonical url
                    // change it!
                    $canonicalUrl = 'http://www.something.com/index.php/component/my-component-name-here/?view=viewNameHere&amp;parameterNameHere=parameterValueUsedInTheViewRightNow

                    $document->_links[$canonicalUrl] = $value;
                    unset($document->_links[$key]);
                    break;                      
                }
            }
        }
    }   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top