This is how I do it:
-
Copy your theme’s
layouts/_default/baseof.html
file into your own layouts/_default folder (most themes doesn’t touch baseof.html and use a different partial, so just follow the trail). -
Add this:
{{ with .Params.relcanonical }} <link rel="canonical" href="{{ . | relLangURL }}" itemprop="url" /> {{ else -}} <link rel="canonical" href="{{ .Permalink }}" itemprop="url" /> {{ end -}}
-
In your post frontmatter you can then add:
relcanonical = "https://example.com/post/"
This is assuming that your theme did not provide its own rel=canonical. If it is already generating its own rel=canonical, you need to find it and remove it because search engines (especially goog) will ignore all rel=canonical if there is more than one per page.
Hope it helps.