Sunday , November 24 2024
Home > Magento Tips & News > Magento 2 Tutorials > Marketing > How to Add Canonical URL in Magento 2

How to Add Canonical URL in Magento 2

magento 2 canonical url

There might be many issues in your websites that you must figure out and eliminate. One of them is that several URLs lead to the same page or duplicate content on different URLs.

Search engine algorithms are able to find out websites that have multiple URLs pointing to the same content and penalize them. The Canonical URL is a great solution to tackle this problem in Magento 2. In this blog, you’ll learn Canonical URL and how to add it in Magento 2.

What Is Canonical URL?

A canonical URL (so-called Rel Canonical) is one of the parts of HTML which informs the main URL being duplicated to search engine tools. A canonical URL is useful in case of duplicate content or many same pages.

Canonicalization is necessary for any page. Therefore, you should declare which one is the main URL. Look at the below example:

domain.com/bestseller/magezonpagebuilder.html

domain.com/extensions/magezonpagebuilder.html

domain.com/magezonpagebuilder.html

Suppose you don’t take any actions on your Magento website. In that case, search engine algorithms will automatically put the random one as a top priority, and you will have no choice until having the chance.

Learn to manage redirects for Magento 2 website through Magento 2 URL Rewrite.

How to Add Canonical URL in Magento 2

To add canonical URLs in Magento 2, firstly, we must identify the type of page you want to custom.

1. For Category and Product

Firstly, log in to your Magento account and go to the Admin panel.

Click on Stores > Settings > Configuration

magento-canonical-url

In the left panel, open the Catalog and Catalog option below. 

magento-canonical-meta-tag

The Search Engine Optimization section appears.

magento-2-canonical-url-product
  • Popular Search Terms is on default mode Enable
  • Product URL Suffix and Category URL Suffix should be .html
  • Use Categories Path for Product URLs: No (Yes)
  • Create Permanent Redirect for URLs if URL Key Changed: Yes
  • Generate “category/product” URL Rewrites should be Yes. Because if you turn it off, the category/product URL rewrites will be removed without the ability to restore.

There are 2 options you will want to change in this section: Use Canonical Link Meta Tag For Categories and Use Canonical Link Meta Tag For Products.

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to build or optimize your Magento stores for sales-boosting? Then visit the Magezon website and grab the necessary add-ons for yours today!

Case 1: Index only Category Page in Magento 2

If you want search engines to index only category pages using category paths, 

  • Turn Use Canonical Link Meta Tag For Categories to Yes.
  • Turn Use Canonical Link Meta Tag For Products to No.
  • Use Categories Path for Product URLs: Yes.

Case 2: Index only Product URL

If you want search engines to index the short product URL and remove the canonical tag for the category in Magento 2

  • Switch Use Canonical Link Meta Tag For Products to Yes.
  • Switch Use Canonical Link Meta Tag For Categories to No.

Case 3: Index both URLs (the best recommendation)

If you want Google (or any other search engine) to index categories and products,

  • Set Use Canonical Link Meta Tag For Products to Yes.
  • Set Use Canonical Link Meta Tag For Categories to Yes.
  • Use Categories Path for Product URLs: No.

Save the changes and refresh the cache at the end.

2. For CMS Page

However, adding a canonical URL for a CMS page in Magento 2 can be a little challenging. From Magento 2.4.3 version, you cannot custom code in the Custom Layout Update anymore. But don’t worry, we will tell you how to do it in this article.

  • To start adding canonical URLs for the cms page in Magento 2, firstly, you need to go to the page you want to add canonical URLs.
  • Then, we must create an .XML file with layout updates and customize it from the backend in Layout Update XML. The filename of XML files should follow the format:

cms_page_view_selectable_<CMS Page Identifier>_<Name for Layout Update>.xml

For example cms_page_view_selectable_about-us_test.xml

After that, fill it in with the following code:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <head>
      <link rel="canonical" src="http://example.com/about-us" src_type="url"/>
 </head>
 </page>

Replace http://example.com/about-us with the canonical URL you want. The code will help you inform search engines of the new URL.

Now, place this file in (your_Magento_root)\vendor\magento\theme-frontend-luma\Magento_Theme\layout, and flush cache using:

php bin/magento cache:flush

Now, there are a few final simple steps. 

Go to the Admin sidebar. Navigate to Content > Pages.

Choose the specific page: About Us. Select Edit.

Go to the Design section. Expand Custom Layout Update and choose the test.

Finally, save the config and refresh the front end. You can check your canonical URL under the <head>, in the page’s source: 

magento-canonical-cms-page-homepage

3. For the Homepage

Similarly, you can also add a canonical tag to the homepage in Magento 2 with the same method as CMS Pages.

Conclusion

I hope this simple guide helps you understand partly Canonical URLs in Magento 2. If you have any questions or ideas, let me know in the comments below. We also have Magento 2 Tutorials, where you can find more tutorials.

At Magezon, we also provide you with diverse, fast, well-coded, yet affordable extensions for your store optimization. Visit our website to opt for the necessary ones!

Optimize Your Magento Store With Powerful Extensions!

Looking for fast, efficient, and well-coded extensions to build or optimize your Magento stores for sales-boosting? Then visit the Magezon website and grab the necessary add-ons for yours today!

About Taylor Nguyen

Taylor Nguyen
I was born and grew up in Hanoi. I got 3 years of experience in Digital Marketing. To me, reading is food, and writing is a release of my energy. Thus, balance in the application of these is an important part of my life.

Check Also

How to Generate XML Sitemap in Magento?

Table of contentsWhat Is XML Sitemap in Magento 2?Who Needs an XML Sitemap?How to Generate …

Leave a Reply