Magento 2 is an incredibly powerful e-commerce platform, but sometimes its default settings can be a bit… restrictive. One such example is the canonical links feature, which, while useful, can be overly aggressive in its canonicalization of URLs. In this article, we’ll explore how to override only a few specific canonical links with a default value of “Yes” in Magento 2, giving you more control over your website’s SEO.
Understanding Canonical Links in Magento 2
Before we dive into the solution, let’s quickly cover what canonical links are and how they work in Magento 2. Canonical links are a way to specify a preferred version of a webpage to search engines, helping to prevent duplicate content issues and improve page ranking. In Magento 2, canonical links are enabled by default, and they’re usually a good thing – but sometimes, they can cause problems.
For instance, if you have a product with multiple URLs (e.g., example.com/product-1
and example.com/category/product-1
), Magento 2 will automatically canonicalize the URLs to the first one, which might not be the one you want to prioritize. That’s where overriding specific canonical links comes in.
Why Override Canonical Links?
There are several reasons why you might want to override specific canonical links in Magento 2:
-
Priority URLs: You might want to prioritize specific URLs over others, especially if they have better SEO performance or are more user-friendly.
-
Canonicalization issues: Magento 2’s canonicalization algorithm might not always get it right, leading to incorrect canonical links that harm your website’s SEO.
-
Custom requirements: You might have custom requirements for specific products or categories that don’t fit with Magento 2’s default canonicalization rules.
Preparing for the Override
Before we start overriding canonical links, make sure you have the following:
-
A working Magento 2 installation (obviously!)
-
A basic understanding of PHP and Magento 2’s module structure
-
A code editor or IDE of your choice (we’ll use Visual Studio Code in this example)
-
A Magento 2 module (we’ll create a new one for this example)
Creating a New Module in Magento 2
To override canonical links, we’ll create a new Magento 2 module. Create a new directory in the app/code
directory and name it (e.g., Vendor_Name/CanonicalOverride
). Inside this directory, create the following files:
app/code/Vendor_Name/CanonicalOverride
├── Composer.json
├── registration.php
├── etc
│ ├── module.xml
│ └── catalog
│ └── attributes.xml
├── Model
│ └── Resolver
│ └── CanonicalUrl.php
└── di.xml
Edit the module.xml
file to add the module’s metadata:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/module_config.xsd">
<module name="Vendor_Name_CanonicalOverride" setup_version="1.0.0">
<!-- ... -->
</module>
</config>
Overriding the Canonical URL Resolver
The next step is to override the canonical URL resolver. In the Model/Resolver
directory, create a new file called CanonicalUrl.php
and add the following code:
<?php
namespace Vendor_Name\CanonicalOverride\Model\Resolver;
use Magento\Catalog\Model\UrlResolver as MagentoUrlResolver;
use Magento\Framework\UrlInterface;
class CanonicalUrl extends MagentoUrlResolver
{
public function getCanonicalUrl($product, $params = [])
{
// Your custom logic to determine the canonical URL goes here
// For example, let's say we want to prioritize URLs with a specific parameter
if (isset($params['specific_parameter'])) {
return $this->getUrl($product, ['_query' => ['specific_parameter' => 'value']]);
}
// If no custom logic applies, fall back to the default behavior
return parent::getCanonicalUrl($product, $params);
}
}
This code overrides the default canonical URL resolver and adds a custom logic to prioritize URLs with a specific parameter. You can add your own custom logic here to fit your requirements.
Registering the Override
To register the override, edit the di.xml
file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\UrlResolver" instance="Vendor_Name\CanonicalOverride\Model\Resolver\CanonicalUrl"/>
</config>
This code tells Magento 2 to use our custom canonical URL resolver instead of the default one.
Enabling the Module
Finally, enable the module by running the following command in your terminal:
php bin/magento module:enable Vendor_Name_CanonicalOverride
Flush the cache to apply the changes:
php bin/magento cache:flush
Testing the Override
Test your override by navigating to a product page that should have a customized canonical URL. Check the HTML source to verify that the canonical link has been overridden correctly.
Conclusion
Overriding canonical links in Magento 2 can be a complex task, but with this guide, you should now have a better understanding of how to create a custom canonical URL resolver and register it in Magento 2. Remember to test your override thoroughly to ensure it’s working as expected.
By following these steps, you’ll have more control over your website’s SEO and be able to prioritize specific URLs for better search engine ranking and user experience. Happy coding!
Module Files | Purpose |
---|---|
Composer.json |
Module metadata |
registration.php |
Module registration |
etc/module.xml |
Module configuration |
Model/Resolver/CanonicalUrl.php |
Custom canonical URL resolver |
di.xml |
Dependency injection configuration |
Note: This article is for educational purposes only and should not be considered as professional advice. Testing and validation of the code are the responsibility of the reader.
Frequently Asked Question
Get ready to master the art of canonical links in Magento 2!
What is the purpose of overriding canonical links in Magento 2?
Overriding canonical links allows you to specify a default canonical URL for specific pages or products, helping search engines understand the hierarchy of your website and preventing duplicate content issues. By default, Magento 2 sets the canonical link to the current page URL, but you can override this to point to a more authoritative or preferred version.
How do I override canonical links for only a few specific products in Magento 2?
You can override canonical links for specific products by adding a custom canonical link in the product’s metadata. To do this, go to the product edit page, click on the “Search Engine Optimization” tab, and enter the desired canonical URL in the “Canonical Link” field. Make sure to save your changes!
Can I set a default canonical link for a category or subcategory in Magento 2?
Yes, you can set a default canonical link for a category or subcategory by going to the category edit page, clicking on the “Search Engine Optimization” tab, and entering the desired canonical URL in the “Canonical Link” field. This will apply to all products within that category or subcategory.
How do I prioritize canonical links in Magento 2 when I have multiple URLs for the same product?
When you have multiple URLs for the same product, Magento 2 will prioritize the canonical link based on the URL’s configuration. You can set the priority by specifying the canonical link in the product’s metadata, or by using a canonical link rewrite rule in the URL rewrite module.
What happens if I don’t set a canonical link for a product in Magento 2?
If you don’t set a canonical link for a product, Magento 2 will automatically generate a canonical link based on the product’s URL. This might not always be the preferred canonical URL, so it’s recommended to set a custom canonical link to ensure search engines understand your website’s hierarchy correctly.