Magento 2 ships with customizable support for SEO - we can define dynamic meta-tags, human-friendly links and generate sitemap. Implementation of these principles is correct, but with Google’s recommendations, we have room for improvement.
Today, we will work on the category view of product listing. Incorrect metadata configuration might lower your e-commerce SEO rank and decrease conversion rate. This is especially true when new customers are your primary audience.
- Magento version: 2.4.2 or newer
Steps to be taken
Robots language comprises simple instructions which should be easy for us to learn. Language based on rules from the classical logic: to index or not to index, same site or duplicated content. It's helpful to understand each directive, so let’s learn their language first. Google SEO documentation is a comprehensive source of knowledge that suits our needs.
Educated and familiarized with robot’s expectations, we can verify the current implementation and prepare the improvement plan. The last step is to create a few modules with event observers and a couple of plugins to change the layout generation.
We won't focus much on technical details - treat this as a challenge and exercise for you or your developers.
What Magento SEO supports
As we stated before, Magento has rich and customizable SEO features:
- user-friendly product and category links
- automated generation of meta description, title, and keywords with placeholders
- sitemap generation with file chunking (50k by default)
- HTTP 301 redirects - vital for store migration
Below is the default configuration available for Magento Luma theme regarding Catalog pages:
- canonical links redirecting to the first page
- missing link rel="prev" and link rel="next"
NOINDEX,NOFOLLOWdirectives silencing previous two
Consider resolving these issues to boost Magento website page rank.
Canonical links in Magento category listing are always redirecting to the first page. This is not a correct behavior for paginated results.
Each page which has optional query arguments should contain a canonical link that points to itself. Canonical link tells indexing robot that this content is the same as on referenced page. This confuses a robot when the second page marks itself as the same as the first page. Each page presents subsequent products, and it should mark its uniqueness with canonical links.
The same rule applies to the filters, sort order and specific layout view (grid or list).
Catalog pages with parameters often have a different content than pages without active filters.
Instruct robot to skip indexing - add
NOINDEX,NOFOLLOW directive and remove canonical link on these pages.
Missing rel links
Magento does not provide implementation for rel next/prev directive - because it’s tricky with the current layout architecture. Layout is rendering blocks in sequence, from the top to the bottom.
Magento calculates pagination for the product listing only after the HTML head is ready. This hinders our task, but don’t worry now. It's crucial to know whether the next page exists or when the current page is the last and rel="next" attribute must be absent. There are events which we can listen to and calculate number of pages for blocks in the header.
Below is a simplified algorithm describing how to generate rel links for the pagination:
- listen to
- verify whether current route id is a category page (
product_list_toolbar_pagerblock from the layout
- get URLs for next and previous page using
- add links to head with
Magento applies filters to the product collection after layout generation, in HTML rendering phase. This means that you must copy toolbar behavior to your class responsible for adding next/prev links.
Toolbar block prepares pagination in
_beforeToHtml instead of
As stated in the
ListProduct block source code, Magento team will fix this implementation in further releases.
For now, we must stick to this hacky solution.
There are SEO agencies that suggest defining
rel attribute in anchor links, but this has no effect and robots skip this attribute.
Incorrect: attribute in the document body
<a href="https://magento2.shop/foo-category?p=2" rel="next">2</a>
Correct: link directive inside document head
To prevent duplicate content, we will apply the same practice here.
Pages with enabled filters, sorting, alternative layout switch (list/grid) must not add this directive. Keep in mind that the first page must not have a prev link and last page must skip link to the next page. For single page results, the first page is the last one.
Incorrect NOINDEX/NOFOLLOW directives
Apart from what we stated before, to skip indexing pages with filters, there is one extra: currency switch - added by a third party module, or by your custom module.
If your store has currency parameter in the URL, then consider which currency is the default and index only that page.
Any additional feature which uses query parameters and has similar content as the original page should have
Again, we're using Page Config class and with plugin on
afterGetRobots method, we can control whether to override default behavior or return our custom directive.
To put it altogether, do not index category page and do not introduce any SEO hints when other parameters than
p (pagination) are present in the URL.
Just stick to these rules:
- do not index parametrised page variations
- canonicals should point to self in general
- remove contradictory hints (canonical and rel next/prev links are not friends of
Contact us if you need help with Magento 2 SEO.