Magento add canonical link to head section via xml

Magento add canonical link to head section via xml

August 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento add canonical link to head section via xml

Adding Canonical Links to Magento Head Section via XML

Are you struggling to add canonical links to your Magento store’s head section? Don’t worry, we’ve got you covered.

Before diving into the code, let’s quickly understand what canonical links are and why they matter.

Canonical links help search engines understand which version of a page is the most important. They prevent duplicate content issues by telling search bots which URL to index and rank. For example, if you have both https://www.example.com and https://example.com, a canonical link ensures that only one version is indexed.

Now, let’s look at the correct way to add a canonical link to your Magento store using an XML layout file.

Step 1: Create or Edit Your Layout File

First, you need to create or edit an existing layout file. In most cases, you’ll be working with local.xml, which is found in the app/design/frontend/package/theme/layout folder.

If this file doesn’t exist, you can create it. Make sure to replace ‘package’ and ‘theme’ with your actual theme package and name.

Open your layout file and look for the <head> section. If there isn’t one, add it inside the <default> tag:

<default>
    <layout version="1.0">
        <reference name="head">
            <!-- Your canonical link will go here -->
        </reference>
    </layout>
</default>

Step 3: Use the Correct Action

The mistake in your provided code is using addLinkRel action instead of remove. The addLinkRel action is used to add a new link, while removeLinkRel is used to remove an existing one. Since you’re adding a canonical link which doesn’t exist by default, you’ll need to use the insertLinkRel action.

Here’s how your code should look:

<default>
    <layout version="1.0">
        <reference name="head">
            <action method="insertLinkRel" rel="canonical" href="http://fonts.googleapis.com/css?family=Dancing+Script"/>
        </reference>
    </layout>
</default>

Step 4: Recompile

After making your changes, make sure to recompile your layouts. You can do this from the Magento admin interface under System > Configuration > Advanced > Developer > Debug.

Best Practices

  1. Always use absolute URLs for canonical links.
  2. If you have an HTTPS site, ensure all canonical links are also HTTPS.
  3. Avoid self-referencing canonical tags unless necessary.
  4. When multiple versions of a page exist (e.g., with and without tracking parameters), use the version that’s best for users.

Conclusion

Adding canonical links to your Magento store is a crucial step in managing SEO effectively. By following this guide, you can ensure that search engines index only the most important version of each page on your site.

Remember, proper canonicalization prevents duplicate content issues and helps search engines understand which pages are most relevant for specific queries.

If you need more advanced help with Magento development or e-commerce strategies, feel free to reach out to Laravel Company. Our team of experienced developers specializes in delivering high-quality solutions tailored to your business needs.

Indexing Themes Configuration Import/Export

Our network