How to Optimize Use Of Canonical URLs In Magento

Introduction

This step-by-step tutorial is designed to help developers and users optimize use of canonical link meta tags in Magento Ecommerce Platform content pages, category pages, and product pages. Search engines such as Google, Bing, Yandex, and Yahoo! impose duplicate content penalties on websites, which may affect their ranking in search results. Canonical URLs help search engines crawl duplicate content only once by indicating the URL that contains most of the duplicate content.

The tutorial involves editing PHP files, therefore some knowledge of PHP and HTML is needed to execute the steps. Also, please remember to make a backup of files that are to be edited.

We also use the “15-Minute SEO Audit” with RankSense free Chrome Browser Extension to detect common SEO issues and help optimize.

Duration To Complete: 20 mins

Prerequisites

Magento Open Source Ecommerce Platform v1.9.1.0

Step 1

In Magento Admin Panel, go to System –> Configuration –> Catalog –> Search Engine Optimizations. Set Use Categories Path for Product URLs and Use Canonical Link Meta Tag For Products to Yes. Here is a snapshot.

With Use Canonical Link Meta Tag For Products set to Yes, Magento automatically generates canonical link meta tags for products pages. With Use Categories Path for Product URLs set to Yes, each product web page can be accessed using two URLs, one with the categories path, for example http://magento.samplestore.biz/mobiles/boogle-texus-6.html and one without, for example http://magento.samplestore.biz/boogle-texus-6.html. Though these are duplicate URLs, the canonical link meta tag eliminates the duplicity.

Press Ctrl + Shft + i in Google Chrome browser to access developer tools. Click on the RankSense tab to use the extension to check canonicalization of a product page as shown in the snapshot below.

Step 2

The canonical link meta tag for categories in Magento is automatically generated depending on an SEO setting in Admin Panel. In Magento Admin Panel, go to the System –> Configuration –> Catalog –> Search Engine Optimizations section. Below is a snapshot that shows Use Canonical Link Meta Tag For Categories set to Yes.

Press Ctrl + Shft + i in Google Chrome browser to access developer tools. Click on the RankSense tab to use the extension as shown in the snapshot below. Analyzing pagination tags for page 2 of a category in our sample Magento store, we find that there is one pagination warning which says Canonical tag is not self-referential. This is a common mistake and means that the canonical tag cannot be the first page in the set.

Set Use Canonical Link Meta Tag For Categories to No. This will disable auto-generation of canonical link meta tags for categories. Below is a snapshot.

In the Magento base directory (in our setup it is /var/www), we modify the code in the template file app/design/frontend/base/default/template/page/html/head.phtml which generates the head part of an HTML page in Magento. It’s best not to modify files in the base theme, so we copy head.phtml to the rwd theme while preserving the folder structure.

The new template file is stored as app/design/frontend/rwd/default/template/page/html/head.phtml. If you already have head.phtml in your theme, then use that template file instead.

Open app/design/frontend/rwd/default/template/page/html/head.phtml in your favorite editor. Add the following code snippet at the end of the file.


$actionName = $this->getAction()->getFullActionName();

if ($actionName == 'catalog_category_view') // Category Page

{

$category = Mage::registry('current_category');

$prodCol = $category->getProductCollection()->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', array('in' => array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG,
    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)));

$tool = $this->getLayout()->createBlock('page/html_pager')->setLimit($this->getLayout()-

>createBlock



$linkNext = false;

if ($tool->getCollection()->getSelectCountSql()) {

if ($tool->getLastPageNum() > 1) {

if (!$tool->isFirstPage()) {

$linkPrev = true;

if ($tool->getCurrentPage() == 2) {

$url = explode('?', $tool->getPreviousPageUrl());

$prevUrl = @$url[0];



}

else {

$prevUrl = $tool->getPreviousPageUrl();

}

}

if (!$tool->isLastPage()) {

$linkNext = true;

$nextUrl = $tool->getNextPageUrl();

}

}

}

if ($linkPrev) echo ''; if ($linkNext) echo ''; echo 'getCurrentUrl() . '" />';

}

This generates self-referential canonical link meta tags for category pages in Magento, and also generates pagination tags.

After implementing Step 2 in the sample Magento store, the resulting HTML of Page 2 of a category is as follows.

<link rel=“prev” href=“http://magento.samplestore.biz/accessories-9.html” />

<link rel=“next” href=“http://magento.samplestore.biz/accessories-9.html?p=3/>

<link rel=“canonical” href=http://magento.samplestore.biz/accessories-9.html?p=2″ />

Analyze using RankSense Browser Extension again as follows. It shows that the issue is fixed.

Check using Google Chrome developer tools. Below is a snapshot.

Conclusion

With these simple steps we have reduced duplication of content using canonical link meta tags for categories and products in Magento. Using RankSense Browser Extension we have ensured that canonicalization is optimized by generating self-referential canonical link meta tags for category pages in Magento.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *