Remove canonical on paginated content in Shopware

How to remove canonical from paginated content to follow technical best-practices. Page 1 is not equal to page 2, and it therefore should not be defined as the canonical URL.

37 views
d

By. Jacob

Edited: 2023-10-05 23:26

To follow technical best practices, URLs containing paginated content should not have canonical pointing to the index page. E.g. Page 2, 3, 4, should not have a canonical that points back to the index. Specifying it anyway is ambiguous and technically inaccurate.

In Shopware, we should either create a plugin on our own, or change our existing child theme plugin if we have one. To do so, we can edit this file:

custom/plugins/MyBeautifulThemePlugin/src/Resources/views/storefront/layout/meta.html.twig

And, you should add the following content:

{% block layout_head_canonical %}
  set requestedUrl = app.request.get('sw-storefront-url') ~ 
  app.request.attributes.get('sw-original-request-uri')

  {% if page.metaInformation.canonical && app.request.query.get('p') is not defined && requestedUrl !== page.metaInformation.canonical %}
    <link rel="canonical" href="{{ page.metaInformation.canonical }}" />
  {% endif %}
{% endblock %}

This should then override the file from the @Storefront:

vendor/shopware/storefront/Resources/views/storefront/layout/meta.html.twig

This code sort of uses a "reverse logic" to include the canonical URL specification only on the pages where it was specifically defined. E.g. In page.metaInformation.canonical; if a page number was supplied (in this case in the p parameter), or if the requested URL matches the URL specified as canonical, we do not include the canonical specification in the code. E.g. We do not want a canonical that equals the requested URL, because that is just redundant.

What's wrong with Canonical on paginated content

Allowing search engines to decide for themselves how to crawl paginated content is actually quite logical and natural when you think about it, because even though you might think these pages does not have any "content" in the sense of having either text, images or video, they are not duplicate.

Even if the pages only contain a collection of text links, the link collection actually does qualify as content on those pages.

A good search engine will understand this distinction, so there is no reason to worry that having a lot of paginated content is somehow going to harm your page.

Additionally, we should get rid of redundant canonical; although they do not do any harm, having a canonical that points to to the same page is just unnecessary, and it adds to the (probably) existing clutter in the HTML.

When should canonical be used then?

Canonical should only be used to specifically tell which URL is the canonical version. E.g. If your CMS is erroneously permitting the use of unused URL parameters, or if your content is otherwise duplicated or syndicated across different URL locations.

Paginated content is not duplicate or low quality content. And to be clear, you also should not use canonical on pages that you deem to have little or "low quality" content. Allow the search engines to decide this for themselves - in any case, canonical is not the right tool to signal whether you think something is insignificant, for that you might opt to use noindex instead; but even still, you will probably be wasting your time doing so.

Tell us what you think:

  1. Sometimes we may to manually clear cached Shopware files to fix namespace issues.
  2. How to obtain the currently selected API language from Shopware vue components.
  3. How to access file system paths from storefront twig files in Shopware.
  4. How to get or change the currently selected sales channel in an Shopware sw-sales-channel-switch component.
  5. In this tutorial you will learn how to work with Shopware entities in a generic way from PHP, without having to specifically inject the repository in your services.xml file.

More in: Shopware