How To Setup a Canonical URL With Jekyll?

I have my site setup, and I am getting ready to make a lot of changes to it, but I need the layout and SEO finished first. After running Lighthouse by Google, I noticed all my SEO tips were good, minus that the Canonical link was pointing to my VPS and not to my site url. After some quick google search, I found out that I could just edit in a for the Canonical:

The Github discussion on the topic

So I did this to my default.html:
https://pastebin.com/XQjrsmAF

All fine and dandy right? No. As now there are two canonical URLs, one pointing to my website, and the other to my VPS…

I tried next to use the Jekyll SEO Tag and using the:
canonical_url: 'example .com'

This would be placed in your layout, I’d presume:
—
layout: default
canonical_url: ‘example .com’
—

<HTML Stuff Here>

Only thing is, that not only does it just add 2 canonical URLs, they’re both the same, just the Jekyll SEO Tag version adds the port number…

I am stumped, and I need some help, any ideas?

To my knowledge the way Jekyll SEO Tag works is it will prepend your page.url with the domain and baseurl (if used) based on the values in your _config.yml

These would be:

url: "http://yourdomain.com"
baseurl: "/some-subfolder" # typically not used unless you host your site in subfolder

The custom canonical_url variable it mentions is for a specific page. You’d put this in it’s YAML Front Matter if you need to override it’s value.

An example of when you’d do this is if you are cross posting a blog post on your site from another source, and want to use that site’s URL instead to send some link juice to that page.

title: My Cross Posted Article
canonical_url: 'https://someotherdomain.com/original-article-url'
1 Like

Thank you for your reply,

So I have done this:

url: "https://example.com"
baseurl: "/" 

Although, I still get my VPS address as the canonical link.

I also tried to remove the s from https, if that had any bearing:

url: "http://example.com"
baseurl: "/" 

It did not have any bearing, and I still have the canonical link as the IP rather than the actual URL

It’s obviously a while ago, but I have the same problem. In _config.yml I have

baseurl: “”
url: “https://michaelbach.de/”

(and the quote chars are not the typographical ones) yet the jekyll-seo-tag generates

<link rel="canonical" href="http://0.0.0.0:4000/misc/">

(where “misc” would be the actual page name). The plugin DOES respond to per-page front matter like

canonical_url: "https://michaelbach.de"

and it generates all the other meta tags correctly, so it’s not broken. I have perused the pertinent document pages http://jekyll.github.io/jekyll-seo-tag/ carefully, compared to the jekyll install new output, reviewed my _config.yml, but no dice. Advice?
Thanking in advance, Michael

If you’re getting URLs like http://0.0.0.0:4000/ when you’re building instead of what is set in url, that is due to not using the proper environment flags.

By default Jekyll builds in development which is why you’re not seeing the production url of https://michaelbach.de prepend your URLs.

Dear Michael:
Thank you very much, but would not this:
production: true in my _config.yml ensure I’m in production? Further, in my makefile it says

JEKYLL_ENV=production bundle exec jekyll build

Ok, it looks like production: true in my _config.yml has not effect on jekyll-seo-tag (??), but

JEKYLL_ENV=production bundle exec jekyll bui

has – not all functions in my makefile had this addition. Solved, thank you!