Setting Noindex and Nofollow on Magento Category Pages: A Comprehensive Guide
In Magento development, it’s often necessary to control search engine behavior for specific pages within your e-commerce store. One common requirement is to set noindex and nofollow meta tags for certain category pages based on specific conditions.
The Current Challenge
You’ve written an observer class (SetRobotsMetaTag) that aims to set the robots meta tag to NOINDEX,NOFOLLOW under two conditions:
- If the current page is a category page.
- If the title of the category page contains the word “parts”.
However, you’re facing an issue with this implementation: the code isn’t checking if it’s on a category page before attempting to access the category object and its name.
The Issue with Current Implementation
The problem lies in the line $category = $this->registry->registry('current_category');. This assumes that the ‘current_category’ registry key is always set, which might not be true for all pages. If this key isn’t set (for example, on a product or CMS page), trying to access $category->getName() will throw an error.
The Solution: Refactoring the Observer
To resolve this issue, we need to first check if the ‘current_category’ object exists before attempting to retrieve its name and apply the noindex rule. Here’s how you can refactor your observer:
public function execute(Observer $observer) {
// Check if current_category is set in registry
if ($this->registry->has('current_category')) {
$category = $this->registry->registry('current_category');
$categoryName = $category->getName();
// Check if category name contains 'part' and action is 'category'
if (stripos($categoryName, 'part') !== false && $this->request->getActionName() == 'category') {
$this->logger->debug("Robots Set for category: {$categoryName}");
$this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
}
}
// If not a category page, set robots to default value (or don't set if you want the default behavior)
else {
$this->logger->debug("Not a category page");
}
}
Important Considerations
Using Stripos for String Comparison
The stripos function is used instead of strpos because it performs a case-insensitive search. This ensures that the rule applies regardless of whether the word “parts” appears in uppercase or lowercase.
Setting Robots Meta Tag on All Pages?
In the refactored code, I’ve removed the initial setting of $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW'); at the start of the execute() method. This is because you might not want to set this meta tag by default for all pages.
If you do want to set a default value (like NOINDEX,FOLLOW), you should add that line back in before the conditional check. However, be aware that setting NOINDEX globally can affect your website’s visibility on search engines.
Magento Robots Meta Tag Behavior
It’s essential to understand how Magento handles the robots meta tag:
- When no meta robot is set, Magento uses the default value stored in the
view/design/config.xmlfile. - If you set a custom meta robot using
$this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');, this will override any existing values and apply to all pages where the observer runs.
Conclusion
Setting noindex and nofollow on specific Magento category pages requires careful handling of the registry keys and conditional logic. By refactoring your observer as shown above, you ensure that the code only attempts to access the category object when it exists, preventing potential errors.
Always remember to test changes thoroughly in a development environment before deploying them to a live site. This will help minimize the risk of impacting your website’s search engine visibility or user experience.
For more advanced techniques and best practices in Magento development, be sure to check out our Magento Development Series on Laravel Company.
Happy coding!