2

I already have canonical links turned on in my CE 1.9 install, but if I didn't I know that I can add a canonical link to a product using custom layout XML this way:

<reference name="head">
<action method="addLinkRel">
<rel>canonical</rel>
<href>https://www.example.html</href>
</action>
</reference>

I would like, however, to customize a particular product's canonical link and I would like to use custom XML to remove the original one before I add in the next so I don't have two links. Is this possible without modifying code?

1

According to /app/code/core/Mage/Page/Block/Html/Head.php method addLinkRel:

     public function addLinkRel($rel, $href)
     {
        $this->addItem('link_rel', $href, 'rel="' . $rel . '"');
        return $this;
     }

it operating with addItem method, which operates with $this->_data['items'] so you can basically use same way removeItem method in your xml :

    <action method="removeItem"><type>link_rel</type><href>https://www.example.html</href></action>
  • Didn't seem to work - except that if I do this: <reference name="head"> <action method="removeItem"> <type>link_rel</type> <href>https://www.example.com/old.html</href> </action> </reference><reference name="head"> <action method="addLinkRel"> <rel>canonical</rel> <href>https://www.example.com/new.html</href> </action> </reference> then all that happens is the order in which the two canonical links are output changes - so that the new link appears before the old. Odd. – dstep Apr 17 '16 at 17:34
  • try to remove unnecessary open/closing </reference><reference name="head"> between actions. alos you can try setText method as <action method="setText"><text><![CDATA[<link rel="canonical" href="https://blog.example.com/dresses/green-dresses-are-awesome" />]]></text></action> – Ilja Apr 18 '16 at 17:26
5

I have been trying to figure this out for quite a while now and none of the solutions I found worked. All I had to do to get it to work was to add another removeItem action with the same link but with ?___SID=U added to the end.

The only reason I can think that this works is that the removeItem action is looking for the URL before the URL rewrite kicks in. Don't quote me on that though :)

<reference name="head">
   <action method="removeItem"><type>link_rel</type><href>{LINK}</href></action>
   <action method="removeItem"><type>link_rel</type><href>{LINK}?___SID=U</href></action>
   <action method="addLinkRel"><rel>canonical</rel><href>{LINK}</href></action>
</reference>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.