Magento – Adding Canonical URL for CMS Pages via Layout XML

Magento does not automatically add canonical URLs for CMS pages. Maybe they will add this feature in the future. However, if you are setting up pages and wish to add a canonical URL using the layout update xml, you can add the following code into the Custom Layout Update XML.
<reference name="head">
<action method="addLinkRel">

19 replies on “Magento – Adding Canonical URL for CMS Pages via Layout XML”

Hi Kathir, this was really useful thanks. I can also use it to add a custom canonical link to product pages. However, do you know of an xml command I can put in the Custom Layout box of a product that would enable me to remove the Magento default canonical link for a product?

You can use the following code to do that. First remove the current URL and then add a new URL.



Hi Sid, Thank you for your help but the removeItem action does not seem to work. The default Magento canonical link remains with the new canonical added underneath. So I end up with two canonical links for a product.

Hi Matt,

remoteItem will work. I tried it on my local install here. Remember… your old canonical url from http till html should be inside the tag. Otherwise Magento will not know which URL to remove. Can you post your URL here?

Did you ever find out how to make it work? The code te remove the old URL isn’t working for me either. I’m 100% sure I haven’t made any typos, it must be some setup or configuration issue..

Did you get any further with this? We’re having exactly the same problem. I’m 100% sure we’re not doing a typo. We’re on Magento CE if that helps.

I faced the same problem. In my case I noticed that the _data array (inside Head.php) was storing the key as URL appended with “?___SID=U”. RemoveItem looks for the exact key in this line of code inside Head.php:
I changed this configuration General > Web > Session Validation Settings > Use SID on Frontend = false. I do not have a multi store setup, hence this change is fine for me.

Thanks a lot! I’ve been searching for hours how to figure out to use
custom canonical tags for productpages in Magento. I also ended up with multiple
tags, but this helps me to remove the default tag. Fortunately it works
on our website. :-)

Just wondering if the same works for CMS pages also?

I can’t get it to work for my CMS page.

Hi thanks for this post, I get an error when I try to implement the code:

Invalid method Mage_Page_Block_Html_Head::addLinkRel(Array
[0] => canonical
[1] =>
#0 [internal function]: Varien_Object->__call(‘addLinkRel’, Array)
#1 [internal function]: Mage_Page_Block_Html_Head->addLinkRel(‘canonical’, ‘http://www.your…’)
#2 /home/pinatas/public_html/app/code/core/Mage/Core/Model/Layout.php(338): call_user_func_array(Array, Array)
#3 /home/pinatas/public_html/app/code/core/Mage/Core/Model/Layout.php(204): Mage_Core_Model_Layout->_generateAction(Object(Mage_Core_Model_Layout_Element), Object(Mage_Core_Model_Layout_Element))
#4 /home/pinatas/public_html/app/code/core/Mage/Core/Model/Layout.php(200): Mage_Core_Model_Layout->generateBlocks(Object(Mage_Core_Model_Layout_Element))
#5 /home/pinatas/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(303): Mage_Core_Model_Layout->generateBlocks()
#6 /home/pinatas/public_html/app/code/core/Mage/Catalog/controllers/ProductController.php(115): Mage_Core_Controller_Varien_Action->generateLayoutBlocks()
#7 /home/pinatas/public_html/app/code/core/Mage/Catalog/controllers/ProductController.php(149): Mage_Catalog_ProductController->_initProductLayout(Object(Mage_Catalog_Model_Product))
#8 /home/pinatas/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(376): Mage_Catalog_ProductController->viewAction()
#9 /home/pinatas/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(248): Mage_Core_Controller_Varien_Action->dispatch(‘view’)
#10 /home/pinatas/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(158): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#11 /home/pinatas/public_html/app/Mage.php(459): Mage_Core_Controller_Varien_Front->dispatch()
#12 /home/pinatas/public_html/index.php(71): Mage::run(‘tiendita’, ‘website’)
#13 {main}

Comments are closed.