Skip to content

Instantly share code, notes, and snippets.

@bennylope
Created February 23, 2012 19:50
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bennylope/1894706 to your computer and use it in GitHub Desktop.
Save bennylope/1894706 to your computer and use it in GitHub Desktop.
Canonical URLs with Jekyll
<link rel="canonical" href="http://yourdomain.com{{ page.url | replace:'index.html','' }}" />
@StarWar
Copy link

StarWar commented Jan 19, 2013

I've set a production_url in config
I use this:

<link rel="canonical" href="{{ site.production_url }}{{ page.url | replace:'index.html',''}}">

@smileprem
Copy link

Thanks guys. I have used this the code by StarWar.

@quangbahoa
Copy link

Here is default config for site.url

<link rel="canonical" href="{{ site.url }}{{ page.url | replace:'index.html',''}}">

@filippovitale
Copy link

In case you set the baseurl on _config.yml

<link rel="canonical" href="{{ site.url }}{{ site.baseurl }}{{ page.url | replace:'index.html',''}}">

@ylogx
Copy link

ylogx commented Jan 28, 2019

If you want to allow links to other domains, you can use following in head.html:


    {% if page.canonical_url != nil %}
    <link rel="canonical" href="{{ page.canonical_url }}"/>
    {% else %}
    <link rel="canonical" href="{{ page.url | replace:'index.html','' | prepend: site.baseurl | prepend: site.url }}">
    {% endif %}

@barbolo
Copy link

barbolo commented Mar 18, 2019

<link rel="canonical" href="{{ page.url | absolute_url }}" />

@mrshannonyoung
Copy link

mrshannonyoung commented Oct 9, 2020

<link rel="canonical" href="{{ page.url | absolute_url }}" />

When on the 'homepage' the URL generated will be /, which is not valid.

I use a variation of this.

{% if page.url =='/' %}{{site.url}}{% else %}{{page.url | absolute_url}}{% endif %}

This prevents an error with Google SEO report on Lighthouse

Document does not have a valid rel=canonicalRelative URL (/)
Canonical links suggest which URL to show in search results.
https://web.dev/canonical/

A caveat though. With jekyll serve {{site.url}} is set to localhost:4000, when you do a jekyll build {{site.url}} is replaced by whatever domain name you set in the _config.yml

@elishadamu97
Copy link

<link rel="canonical" href="{{ page.url | absolute_url }}" />

When on the 'homepage' the URL generated will be /, which is not valid.

I use a variation of this.

{% if page.url =='/' %}{{site.url}}{% else %}{{page.url | absolute_url}}{% endif %}

This prevents an error with Google SEO report on Lighthouse

Document does not have a valid rel=canonicalRelative URL (/)
Canonical links suggest which URL to show in search results.
https://web.dev/canonical/

A caveat though. With jekyll serve {{site.url}} is set to localhost:4000, when you do a jekyll build {{site.url}} is replaced by whatever domain name you set in the _config.yml

Please where do I place this code in my Jekyll website?

@elishadamu97
Copy link

Please where do I place this code in my Jekyll website?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment