Sunday, 15 September 2013

joomla - Modify the existing canonical link in header -



joomla - Modify the existing canonical link in header -

i using joomla 2.5 , want alter canonical link in header. 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 right canonical, leaves old canonical link there have 2 canonical links in header.

how can alter or delete old canonical link?

what want instead 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'] $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 grab of current head info document, including canonical link want replace. search through custom set (where i'm guessing be) , if finds it, replace yours. if doesn't find it, tacks on @ end. in case.

potential problems can see right away:

if tag contained rel='canonical' single quotes not found, may have adjust that. the tag may have been placed in different section of i've termed $doc_data. may want var_dump($doc_data}; confirm location of variable in array.

joomla joomla2.5 canonical-link

No comments:

Post a Comment