I´m new to Magento but how can I set my canonical tags for my products, so that Product A can be in 4 categories but still link to one at the end. And I don´t have duplicate content.

2 Answers 2


Go to System -> Configuration -> Catalog -> Catalog then Search Engine Optimizations and there is Use Canonical Link Meta Tag For Categories and set it to yes.


Ofcourse you can set canonical URL by going to System -> Config -> Catalog -> Search Engine Optimisation -> Canonical Link Meta Tag For Categories and set this to yes but if you want to choose the specific category then you need to customise the code. Here is the step by step instructions -:

Step 1 - Create a new attribute (category dropdown) against the product

Step 2 - Here is the code to get the list of selected categories against the product

     * get list of categories associated with the product
    public function getCategories() {
        $categoryOption = array('' => Mage::helper('yourhelper_canonical')->__('Please select primary category'));
        $categoryIds = array();
        $excludedRootCategories = array();

        //if product exists then retrieve only associated categories 
        $product = Mage::registry('current_product');
        if ($product) $categoryIds = $product->getCategoryIds();

        //getting all the categories for the particular store and sorting them by level and parent_id
        $categories = Mage::getModel('catalog/category')->getCollection()
                            ->addAttributeToSort('level', 'ASC')
                            ->addAttributeToSort('parent_id', 'ASC');

        //if product has categories then load only those categories
        if (!empty($categoryIds)) $categories = $categories->addAttributeToFilter('entity_id', array('in' => $categoryIds));

        //load category collection at the end to apply all the above filters

        //loop through loaded categories collection 
        foreach ($categories as $cat):
            //loading collection with all the parent category ids using getPathIds, in simple terms it will load all parent categories associated with this category 
            $collection = $cat->getResourceCollection();
                ->addAttributeToFilter('entity_id', array('in' => $cat->getPathIds()))
                ->addAttributeToSort('level', 'ASC');

            $fullCategoryPath = '';

            //concatenating the whole path using name instead of id 
            foreach ($collection as $col) {
                if (strlen($col->getName())) $fullCategoryPath .= $col->getName().' -> ';
            if ($fullCategoryPath) $fullCategoryPath = substr($fullCategoryPath,0,strlen($fullCategoryPath)-4);

            if (strlen($fullCategoryPath)>0){
                $temp = array('value' => $cat->getId(), 'label' => $fullCategoryPath);
                array_push($categoryOption, $temp);

        return $categoryOption;

Step 3 - Extract the product URL using the above attribute using the following code

    protected function getProductUrl($product)
        $category_id = Mage::getResourceModel('catalog/product')->getAttributeRawValue($product->getId(), 'new_category_drop_attribute', $product->getStoreId());
        if ((int)$category_id>0){
            $category = Mage::getModel('catalog/category')->load($category_id);
            $url = str_replace('.html','',$category->getUrlPath()).'/'.$product->getUrlPath();
            $url= $product->getUrlPath();
        return $url;

This way you can get the specific category you want to be used in the canonical URL tag. This approach will help you more in optimising your website for search engines.

If you are a Magento developer then you can implement the above solution but if not then probably downloading the following extension might be useful


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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