drupaldrupal-9

Drupal 9 - How to alter pagination link from /hub/blog?page=0 to /hub/blog


I look after a Drupal 9 website that has a blog that shows pagination at the bottom of the listing page.

The blog listing page can be accessed using the path /hub/blog, but then the first page link in the navigation points to /hub/blog?page=0. An SEO company has asked if we can make the pagination for the first page link to /hub/blog (without the page parameter). I believe this is because there ends up being two different URL's that show the same content and that's not great for SEO?

How can I re-write the first pagination link so as to remove the query parameter from the URL? I'm assuming I need to do it in the .theme file somehow?


Solution

  • One solution would be to use ajax.

    But aside from that, you can override the twig template for the pager to do a logic check to see what the href is on the pager link and if it's page=0 remove the query param.

    Assuming you're using classy theme, this is what you could do after overriding navigation/pager.html.twig

    {% for key, item in items.pages %}
          {# ... #}
          <a href="
          {% if item.href|slice(-6) == 'page=0' %}
          [your-page-alias-here]
          {% else %}
          {{ item.href }}
          {% endif %}
          " title="{{ title }}"{{ item.attributes|without('href', 'title') }}>
           {# ... #}