Magento Store: How to Add Nofollow Tags to Layered Navigation
Layered navigation is an essential feature for enhancing user experience on e-commerce platforms like Magento. It allows customers to filter products based on various attributes such as price, brand, color, etc. However, there may be instances when you want to indicate that certain links in the layered navigation should not be followed by search engines.
In this guide, we’ll walk you through the process of adding nofollow tags to your Magento layered navigation. Let’s dive right in!
Understanding Nofollow Tags
Before we proceed with the implementation, it’s crucial to understand what nofollow tags are. These are HTML attributes that tell search engine crawlers not to follow certain links on your website.
By default, when a user clicks on a link, the linked page is crawled and indexed by search engines. However, if you add the rel=“nofollow” attribute to a hyperlink, search engines will ignore that link while crawling the site.
Which File to Modify
The file we need to modify depends on your Magento version:
For Magento 2.x
In Magento 2.x, the layered navigation is primarily managed through the app/design/frontend/Magento/layerednavigation module. To add nofollow tags to the layered navigation links, you’ll need to make changes in the relevant template files within this folder.
Open the catalog/product/list.phtml file located under your theme directory (app/design/frontend/<your_vendor>/<your_theme>). Look for the code responsible for generating the layered navigation filter blocks. You will find something similar to the following:
<?php $_filterBlock = $block->getChildBlock('catalog.filter'); ?>
<?php if ($_filterBlock): ?>
<?php $_filterBlock->setTemplate('Magento_Catalog/templates/layer/filter.phtml') ?>
<?php echo $_filterBlock->toHtml() ?>
<?php endif; ?>
For Magento 1.x
In Magento 1.x, the layered navigation is handled through the app/design/frontend/<your_vendor>/<your_theme> folder. Similar to Magento 2, you’ll need to locate and modify the relevant template file responsible for generating the layered navigation links.
The primary file that controls this functionality is typically named catalog/layer/view.phtml. Open this file in your preferred text editor or IDE.
Adding Nofollow Tags
Now let’s see how to add nofollow tags to the layered navigation links:
Magento 2.x
In the list.phtml file, locate the section where the filter options are printed. It usually looks something like this:
<div class="block-content">
<ul>
<?php foreach ($_filters as $_filter): ?>
<li<?php if (isset($_filter['is_expanded']) && $_filter['is_expanded'] == true): ?> class="active"<?php endif; ?>>
[...]
<a href="<?php echo $this->getFilterUrl($_filter) ?>">
<?php echo $_filter['label'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
To add the nofollow attribute, you simply modify the href attribute of the anchor (<a>) tag by adding rel="nofollow":
<a href="<?php echo $this->getFilterUrl($_filter) ?>" rel="nofollow">
<?php echo $_filter['label'] ?>
</a>
Magento 1.x
In the view.phtml file, search for the code responsible for displaying the layered navigation filters. You’ll find a loop similar to this:
<?php if (count($this->getFilters())): ?>
<div class="block-content">
<?php foreach ($this->getFilters() as $filter): ?>
[...]
<a href="<?php echo $this->getFilterUrl($filter) ?>">
<?php echo $this->escapeHtml($filter->getName()) ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
To add the nofollow attribute, update the href attribute of the anchor tag:
<a href="<?php echo $this->getFilterUrl($filter) ?>" rel="nofollow">
<?php echo $this->escapeHtml($filter->getName()) ?>
</a>
Conclusion
In this blog post, we explored how to add nofollow tags to layered navigation links in Magento. By following the steps outlined above for your specific Magento version, you can easily apply these changes within your theme files.
Remember, while adding nofollow tags is a common SEO practice, it’s essential to use them judiciously and not overdo it, as this may impact your website’s crawlability and indexing. Always ensure that you understand the implications of nofollowing links before applying them to your production environment.
At Laravel Company, we specialize in Magento development services, including SEO optimizations like the one discussed here. If you need expert assistance with your e-commerce store’s technical aspects or customization, feel free to reach out!
Happy coding!