Paginated pages don't show the correct URL in the meta canonical tag

Paginated pages don’t show the correct URL in the <link rel="canonical" href=""> meta tag.

For example, https://example.com/page/2/ shows <link rel="canonical" href="https://example.com"> instead of <link rel="canonical" href="https://example.com/page/2/">.

How can I fix this?

On this moment, I only use <link rel="canonical" href="{{ .Permalink }}">.

Have you tried to use something like this:

{{ if (eq .Kind "section") }}
  <link rel="canonical" href="{{ .Paginator.URL | absURL }}">
{{ end }}

You could even add:

{{ if .Paginator.HasPrev }}
  <link rel="prev" href="{{ .Paginator.Prev.URL | absURL }}">
{{ end }}

{{ if .Paginator.HasNext }}
  <link rel="next" href="{{ .Paginator.Next.URL | absURL }}">
{{ end }}

How can I use that with: <link rel="canonical" href="{{ .Permalink }}">?

My website’s template has <link rel="canonical" href="{{ .Permalink }}">.

{{ if (eq .Kind "section") }}
  <link rel="canonical" href="{{ .Paginator.URL | absURL }}">
{{ else }}
  <link rel="canonical" href="{{ .Permalink }}">
{{ end }}

Please, read Build the navigation for more information.

It’s not working.

I tried the following:

{{ if .Paginator}}
<link rel="canonical" href="{{ .Paginator.URL }}">{{else}}
<link rel="canonical" href="{{ .Permalink }}">
{{ end }}

And it seems to be working.

Is this a good way to do this?

I don’t know how your site works. That’s why I asked you to read the documentation, as I said you could use “something like this”.

And you did.