Canonical URL for domain name and backup site

I have a site with a domain name. I compile it locally and deploy it with rclone.

Obviously, there’s a baseURL configured. And a head canonical link:

<link rel="canonical" href="{{.Permalink}}">

When I upload the sources to GitHub, I use an action to have a copy of the site online.

My GitHub Action is, apart from a few details, the one suggested in Hugo’s documentation.

baseURL is passed as a command line option here and it works perfectly:

hugo --baseURL "${{ steps.pages.outputs.base_url }}/" 

The problem is that I’d like to be the same on both sites. So that the GitHub Pages site also refers to the original domain name. I’m interested in your advice on how to achieve this simply.

If I understood it right:

currently you have two deployments with different domain names hosted by different servers. And you want your site to be available using both domain names and these sites are identical

You will have to publish the site only once and let’s say “redirect” the site from one server to the other.

As far as I know, you cannot redirect a Github Pages site to a custom server. But the other way round is possible, if your domain hosting sevice supports it.

So the solution would be:

  • publish your site to github pages https://username.github.io/repo
  • redirect your custom domain www.my-domain.com to the one above. (usually using a verified CNAME)

That way regardless of the used URL when accessing the site:

  • https://username.github.io/repo
  • https://www.my-domain.com

it will be always shown as: https://www.my-domain.com

Here’s a link to the official documentation:

There are a couple of tutorials around search for “github pages custom domain name”

1 Like

Yes, these 2 sites are identical. And I would like to be able to access one or the other. This is already the case and it’s perfect.

No, I want to publish this site twice. And I don’t want any redirects. The idea is that the GitHub Pages version remains completely accessible if my domain name or my hosting disappears.

In other words, these 2 pages:

https://nfriedli.github.io/nicolasfriedli.ch/
https://nicolasfriedli.ch/

should have the same canonical tag:

<link rel="canonical" href="https://nicolasfriedli.ch/">

What I don’t want is a referencing of the site, which is hosted on GitHub Pages. And I think canonical is more elegant (and more correct) than blocking with noindex or a robots.txt file.

NB 1: The noindex solution is trivial:

{{ if not (hasPrefix .Permalink "https://nicolasfriedli.ch/") }}
    <meta name="robots" content="noindex">
{{ end }}

NB2: I think we need to do something like (pseudocode):

if baseURL != nicolasfriedli.ch
    canonical = nicolasfriedli.ch/{{.RelPermalink}}
else 
    canonical = {{.Permalink}}

This matter isn’t actually that complicated. I’ve set baseURL = "https://a.com/" in hugo.toml and deployed the website simultaneously on Vercel, Cloudflare Pages, and my own VPS, using a.com, b.com, and c.com respectively.

In daily use, the latter two don’t seem much different, except that a few fixed links actually point to resources on a.com. If a.com unfortunately goes down, I can just manually change the baseURL.

Having multiple domain names for the same website content doesn’t really serve much purpose anyway.

That’s not really the issue here. The idea is not to multiply domain names, but to have an already viable backup.

And that if links point to it, for example in an emergency situation, the correct canonicals help the main site’s SEO.

My solution:

<link rel="canonical" href="{{ site.Params.canonicalDomain}}{{ .RelPermalink }}">

{{ if not (hasPrefix .Permalink site.Params.canonicalDomain) }} 
  <meta name="robots" content="noindex">
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.