How to get the current absolute URL?

I’m trying

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

But it’s returning me the relative URL… I don’t get it

PS: I’m actually upgrading hugo from an old version… I don’t want to canonicalize all URLs but sometimes I want an absolute URL (with the host name in it)

So .Permalink is already the absolute URL. You probably have a setting in your config file for relative URLs. Show us your config file.

1 Like

I have a super simple configuration file

baseURL = "http://domain.com/"
languageCode = "en-us"
title = "Domain"
pluralizeListTitles = false
preserveTaxonomyNames = true
ignoreFiles = [ "\\.original.jpg$" ]

[author]
  name = "Me"

[taxonomies]
  tag = "tags"

All I want is the absolute URL with the host name in it.

Hmm. What Hugo version are you using?

The last one

Hugo Static Site Generator v0.54.0/extended darwin/amd64 BuildDate: unknown

This is driving me crazy…

Even with a param in the configuration the host name is… removed?!

[params]
  hostName = "http://domain.com"
<link rel="canonical" href="{{ .Site.Params.hostName }}{{ .Permalink | absURL }}">

The only way is to actually write the host name MANUALLY like that

<link rel="canonical" href="http://domain.com{{ .Permalink | absURL }}">

But I can’t do that… I have multiple host names by environments (production, test, dev, etc.)

There is something else going on here. Could you share your full site code?

1 Like

In short:

  • set baseURL in top level settings in your config
  • use .Permalink

I can’t share my full site code… it’s big and private

I have a warning in my code… is it possible that it may cause some troubles?

Building sites … WARNING: calling IsSet with unsupported type "invalid" (<nil>) will always return false.

Did you solve the problem described in the title?

No I style have the problem as described in the title.

I’m trying workaround using site params {{ $.Site.Params.hostName }} and I don’t understand how is hugo even not outputting the param at all. I’m getting esoterical and I’m checking any potential clue (the warning maybe?)

The warning isn’t relevant to this issue. I suggest you look at my post above and compare that to the documentation and the many example sites on the web.

That’s what I’ve done

This is my config file

baseURL = "http://domain.com/"
languageCode = "en-us"
title = "Domain"
pluralizeListTitles = false
preserveTaxonomyNames = true
ignoreFiles = [ "\\.original.jpg$" ]

[author]
  name = "Me"

[taxonomies]
  tag = "tags"

[params]
    hostName = "http://domain.com"

And in a layout/partials/header.html I’m doing thoses

<link rel="canonical" href="{{ .Permalink }}">
<link rel="canonical" href="{{ .Permalink | absURL }}">
<link rel="canonical" href="{{ .Site.Params.hostName }}{{ .Permalink | absURL }}">

And none of them are getting me absolute URLs with the host name in it

That’s exactly what you’ve suggested (and what the docs says)

You are using:

But that is not exactly what was posted above:

Hint: use the exact case for baseURL https://gohugo.io/getting-started/configuration/#all-configuration-settings

Also if your problem remains unresolved after doing the above then you really need to share your project with dummy content for us to see the full context.

I’m already using the exact case baseURL, I also was thinking that was the problem, but unfortunately it’s not

PS: I was using baseurl with hugo 0.17 without any problem… anyway I’ve already switched to baseURL and I still have the problem

Try removing the cache i.e. hugo server --ignoreCache

Beyond that I cannot reproduce your problem locally with the config and the template code you posted above.

Thank you all… I will take some rest and later try to make a minimal reproduction case.
I’ll keep you all updated

I don´t know what you are trying to do, if you are trying to do imports like in the example you could use relative urls instead absolute ones, this way is the most common since is possible to change the location of the files all together.

If you are trying to do somethin more advanced another option is to get the url from js: https://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/