Your sites Log in Sign up Menu

Canonical URLs?

Google Search Console mentioned a lot of pages without user-selected canonical, and after researching a little, I learned that it's better for static sites to mention each original URL (entry or page) as a link rel canonical in the head. I think that's what is better, not sure.

I managed to do this perfectly for entries, but using {{{absoluteURL}}} in the head, between {{#entry}} and {{/entry}}. But this doesn't work for the site homepage, or pages like archives, etc.

Is there a Reference item that would work for every page of the site, and not just entries?


a year ago, 7 replies   developers   Improve this question

There isn't a super easy way to do this. I'd recommend creating a new partial template called 'canonical', use it in your template's head.html file like so:

<link rel="canonical" href="{{> canonical}}" />

Then you can add the value "canonical" to the partials property of each view in your template's configuration file: package.json.

I might build this into the templates actually! Will follow up here once I do.

Answered a year ago · Improve this answer

Ah indeed, it seems to work! Thanks!

It's much better than my previous solution where I added custom {{head}} endings depending on pages.

One thing I've still haven't figured out, and I know it's a head scratcher with canonicals, but what to do with "/page/2" for the homepage of the blog? I'd guess it's normal for 2nd pages to have the 1st page as the canonical, but since they are always changing, they'd need their own.

Thanks again, looking forward to your implementation in the templates so I can steal from them :)

Answered a year ago · Improve this answer

Ah indeed, it seems to work! Thanks!

It's much better than my previous solution where I added custom {{head}} endings depending on pages.

One thing I've still haven't figured out, and I know it's a head scratcher with canonicals, but what to do with "/page/2" for the homepage of the blog? I'd guess it's normal for 2nd pages to have the 1st page as the canonical, but since they are always changing, they'd need their own.

Thanks again, looking forward to your implementation in the templates so I can steal from them :)

Answered a year ago · Improve this answer

For the "entries.html" partial template, you can do something like this:

"canonical": "{{{blogURL}}}{{#pagination.current}}/page/{{pagination.current}}{{/pagination.current}}"
Answered a year ago · Improve this answer

Ah thanks so much! is there a way to configure this so that the homepage (page1) keeps the main/root URL as its canonical, and only have /page/2,3,4 for the following pages?

Answered a year ago · Improve this answer

Good point, does this work?

"canonical": "{{{blogURL}}}{{#pagination.previous}}/page/{{pagination.current}}{{/pagination.previous}}"
Answered a year ago · Improve this answer

Amazing, this works perfectly. Thanks!

Answered a year ago · Improve this answer