Search code examples
magentomagento-1.6canonical-link

Setting canonical tags for categories in Magento


Is this possible, or is there some code that can be added so I can set a different canonical URL for categories in Magento? I can set for products fine.


Solution

  • Out of the box there is nothing for this that I am aware of. You will need to develop or build your own method of doing this.

    You will need to create an entry in a layout.xml file to put an additional template in the head section of the page when you are on a category page (this would likely be in a catalog_Category_view block). You would also probably need a view file as well as a Block object to fetch the URL you want to use (technically you could put that in the view file, but having the block object is more "Magento").

    Your layout.xml block would look something like this

    <catalog_category_view>
        <reference name="head">
            <block type="canonical/canonical" name="head_url" as="head_url" template="ocaff/canonical/head.phtml" />
        </reference>
    </catalog_category_view>
    

    This block references a head.phtml file. That file would contain:

    <link rel="canonical" href="<?php echo $this->getCanonicalUrl() ?>" />
    

    This calls back to a block object that has a function called getCanonicalUrl(). In this function you will find and determine what you want the canonical to be. Most likely you want this to be the URL key, but you may have other logic in mind there.