Introduction
This step-by-step tutorial is designed to help developers and users indicate paginated content in the Magento Ecommerce Platform collection of products. Google search engine will be able to treat multiple pages of products as a single entity and understand that the pages are a logical sequence. Hence, the value of inbound links will not be diluted among the pages.
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 which 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: 30 mins
Prerequisites
Magento Open Source Ecommerce Platform v1.9.1.0
Step 1
In 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.
Step 2
Open app/design/frontend/rwd/default/template/page/html/head.phtml in your favorite viewer.
$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(‘catalog
/product_list_toolbar’)->getLimit())->setCollection($prodCol);
$linkPrev = false;
$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();
}
This will show rel=”prev” and rel=”next” link tags to indicate paginated content of categories of products. If it’s the first page then only the rel=”next” link tag is indicated. If it’s the last page then only the rel=”prev” link tag is indicated. For all other pages, both link tags are indicated. The number of pages must be greater than one in order to indicate the link tags.
Here is the resulting HTML for page 2 of a category in our sample Magento store.
<link rel=”prev” href=“http://magento.samplestore.biz/accessories-9.html” />
<link rel=”next” href=“http://magento.samplestore.biz/accessories-9.html?p=3” />
Check using developer tools in Google Chrome browser by pressing Ctrl + Shift + i. Below is a snapshot.
Step 3
Install “15-Minute SEO Audit” with RankSense free Chrome Browser Extension. This browser extension looks for common SEO issues in the current web page. It adds automatic SEO issue detection to the Chrome Developer Tools. We use it in this step to detect issues with the pagination tags.
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. We provide a fix in the next step.
Step 4
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.
We fix the pagination warning detected by RankSense Chrome browser extension in Step 2. The fix is that the canonical link meta tag should be self-referential. Currently it points to the first page only.
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.
Step 5
Open app/design/frontend/rwd/default/template/page/html/head.phtml in your favorite editor. Add the following snippet …
echo ‘<link rel=”canonical” href=”‘ . Mage::helper(‘core/url’)->getCurrentUrl()
. ‘” />’;
… to the code mentioned in Step 2 as shown below.
<?php
$actionName = $this->getAction()->getFullActionName();
if ($actionName == ‘catalog_category_view’) // Category Page
{
$category = Mage::registry(‘current_category’);
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
(‘catalog/product_list_toolbar’)->getLimit())->setCollection($prodCol);
$linkPrev = false;
$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();
}
}
This generates self-referential canonical link meta tags for category pages in Magento, and also generates pagination tags.
After implementing Step 4 and Step 5 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 as follows. It shows that the pagination issue detected in Step 3 is fixed.
Check using Google Chrome developer tools. Below is a snapshot.
Conclusion
With these simple steps we have indicated to Google not to dilute the value of inbound links among pages and treat all pages belonging to a category as a single entity. Using RankSense Browser Extension we have ensured that pagination is optimized by generating self-referential canonical link meta tags for category pages in Magento.