How to implement rel=prev and rel=next to Magento’s pagination?

How to implement rel=prev and rel=next to Magento’s pagination?

As most of you know that “When dealing with online stores with a lot of products, pagination on category pages can get really problematic for search engines” like Toni Anicic wrote in his article. I don’t want to repeat his words, but to show you how you can add rel=”prev” and rel=”next” link tag attributes in the head tag for pages, which will boost your SEO. This peace of code is already provided by Magento community, but this is improved version.

Tested in Magento CE


1. So, if you didn’t already modified head.phtml file, create identical directory hierarchy and copy/paste head.phtml in your theme or package.

Path example if using package:

Path example if using theme:

2. Add code below to head.phtml. I’ve added code at the bottom of 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('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();
            if (!$tool->isLastPage()) {
                $linkNext = true;
                $nextUrl = $tool->getNextPageUrl();
    if ($linkPrev) echo '<link rel="prev" href="' . $prevUrl . '" />';
    if ($linkNext) echo '<link rel="next" href="' . $nextUrl . '" />';


Below is a result if you are on page 3.

<link rel="prev" href="">
<link rel="next" href="">

Search engine optimization in Magento’s Configuration

After implementing rel=”prev” and rel=”next” you need to re-config Magento’s SEO options, which means that you don’t need anymore Canonical Link Meta Tag For Categories. Below is a example how we setup Magento’s SEO options for one of our clients.

Hope this text will help you. And if you are migrating from Magento 1 to Magento 2 there is a handy SEO checklist that will also help you.

You made it all the way down here so you must have enjoyed this post! You may also like:

Build your 2017 Magento SEO strategy with these tips Ivona Namjesnik
Ivona Namjesnik, | 10

Build your 2017 Magento SEO strategy with these tips

Free eCommerce SEO consultations by Inchoo at Meet Magento Poland Tea Pisac Benes
Tea Pisac Benes, | 0

Free eCommerce SEO consultations by Inchoo at Meet Magento Poland

In SERP title tags no longer control what’s shown Toni Anicic
Toni Anicic, | 10

In SERP title tags no longer control what’s shown


  1. Please be careful when using this code, as it causes a security vulnerability if you give certain parameters via GET.
    For example:

    ?limit=24"><img src%3da onerror%3dprompt(/XSS/)>

    Please escape the $linkNext variable for example like:

    if ($linkNext) echo '<link rel="next" href="' . htmlspecialchars($nextUrl, ENT_QUOTES, 'UTF-8') . '" />'; 
  2. I would’ve excepted more from a “Professional Magento Solutions Partner”.
    Coding Standards exists for a reason and this code example doesn’t belong into template files.

    1. Besides the fact it doesn’t belong in templates, it’s most likely the worse implementation one could do. Besides the fact that it does’t even take in consideration if there active filters, there are better ways to do it WITHOUT doing an extra unnecessary sql query and collection hydration.

  3. great code but dont work for category filters

    Prefer this solution to remove p=1 (to not remove filters):
    if ($tool->getCurrentPage() == 2) {
    $replace = array(‘?p=1’, ‘&p=1’);
    $urlData[‘prev’] = str_replace($replace, ”, $tool->getPreviousPageUrl());

    Now searching to have the correct last page number with filters, must not be very difficult.

  4. whats the best way to implament rel = “next/prev” if we have filters?
    the filtered view results in paginated content.
    the filtered view has different urls: exmple:
    right now we have canonical tag all to the main category page.

    look at what it says in searchengineland:
    look at Advanced Techniques paragraph. do you agree? it seem like google will index the page multiple times.

    what would you recommend to do in this case?

  5. I’ve added this to head.phtml but nothing happens. Is this a cache issue or are you supposed to add this to the template phtml that was created when you set up the block tag on the product page?

  6. Hello,
    This code is working properly except when we filter product on category page. Can you please help me to solve this problem?


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.