SEO fix to add page number to canonical link to listing pages pagination
Thanks, this was a great starting point! it didn't work exactly for our needs but i've modified it a bit to work, we intended to achieve: --> <link rel="canonical" href="sample/blog/"> (for page 1, point to original page instead of the generated page 1 (duplicate)) --> <link rel="canonical" href="sample/blog/page/2"> (for page 2 and so on..)
also, if you have a simple list page it might be safer to do {% if simple_list_page %} // instead of {% if request.path is string_containing '/all' %} // might have conflict if the page url has '/all...'
I'd also like to add this version if you are interested:
<!-- `is_listing_view` is a HubL variable in the developer info pane that checks if the page is a listing view such as blog listing page -->
{% if is_listing_view %}
<!-- Add Canonical to paginated pages, topics, authors, and main blog listing page -->
<link rel="canonical" href="{{canonical_url}}">
<!-- Nested IF statement. -->
{% if current_page_num > 1 %}
<script>
//Append page num to title so you don't have duplicate title tags
document.title += " | Page {{current_page_num}}";
// Replace Current Meta Description with new one that appends page num before it.
document.querySelector('meta[name="description"]').setAttribute("content","Page {{current_page_num}} - {{content.meta_description|escapejs}}");
</script>
{% endif %}
<!-- /Nested IF Statement -->
{% endif %}
<!-- /End listing view IF -->
Your code should fix pagination issues but you also may want to change duplicate titles and descriptions. Please let me know what you think about this? We have implimented this fix and it works like a charm.