Thomas' Tech Tips

Canonical URLs in Jekyll templates

30 June 2023 - Thomas Damgaard

I recently had to fix some issues in Jekyll in which on my site because the canonical links were broken. Instead of linking to an URL ending in / they linked to index.html which works fine but is not actually the canonical link.

The easiest fix I thought up was to replace the string index.html in the URL.

This can be done using | replace:'foo','bar', which will replace foo with bar.

This is how I solved the canonical URL issue:

<link rel="canonical" href="{{ site.url }}{{ page.url | replace:'index.html',''}}">
Filed under: howto, jekyll, liquid, seo, template, tips, web

Back to article list