HUGO

How can I force HUGO to generate relative paths for everything?

I’m trying to run my hugo deployment in a dev server, but it keeps generating absolute URLs for everything. When I look at the source it generates, I see “//localhost:1313” prepended to everything, and when I try to access via the docker image (which maps 1313 to port 80), it breaks. And if I change the config to make it work locally, it breaks in production because it’s generating the wrong base url. What I want it to do is omit the hostname entirely, leaving it relative so that the browser can find it no matter where or how it’s hosted. I’ve tried setting baseurl, relativeurls, uglyurls, but nothing seems to make it output relative urls.

I don’t know how to set the baseURL as you describe, I just set mine to the full domain of the site it is for and it works, but you are using Docker as a dev env, so that won’t work; instead I suggest configuration directories for different environments: https://gohugo.io/getting-started/configuration/#configuration-directory

You can have your Docker run the config that sets the URL as you want it. :slight_smile:

Well if you control your template files just make sure to use .RelPermalink whenever your print a permalink.

This is unfortunate, because it means that one needs to have different configurations for different environments, which means that bugs will creep into the live site because the config that goes live can’t be tested in dev or CI.

Is there really no way to make all URLs relative in HUGO? It’s a fundamental part of the HTML spec, and exists specifically to avoid requiring separate builds, or code changes just because you moved the site. I would have expected relative links to be the default behavior…

Regis answered, but to clarify: I’m sure it’s possible, just not something I use personally.

The instructions at https://gohugo.io/content-management/urls/#relative-urls should work. If they don’t, then you’ll need to share a reproducible project for us to look at.

Oh yes! I didn’t know about that. Thanks for pointing that out.

Sure, just follow the quick start instructions off the main site. I can’t post a link due to discourse rules.

hugo new site quickstart
cd quickstart
git init
git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke
echo 'theme = "ananke"' >> config.toml
hugo new posts/my-first-post.md
hugo server -D

Now view it with a browser and view source. You’ll see instances of “localhost:1313” in the html code (for example, href=“http://localhost:1313/posts/my-first-post.html” instead of href=“posts/my-first-post.html”). Changing all instances of .Permalink to .RelPermalink in themes/ananke/layouts.index.html doesn’t fix it, nor does adding baseurl, relativeurls, or uglyurls to config.toml.

That’s because you are running the dev server.

With relativeURLs = true and .RelPermalink:

  • run hugo,
  • inspect the public folder
  • and look at the generated urls.

The documentation makes no mention of this behavior: https://gohugo.io/commands/hugo_server/

In fact, it says that “many run it in production”.

My bad, I didn’t update the docs well enough. Please submit any changes you’d make to the description and submit the changes; we’re in this together! :sunglasses:

I think Hugo renders relative URLs by default. My website doesn’t use relativeURLs and I get relative links.

There might be a mistake in the documentation, because the defaults for these two options seem in conflict with each other:

canonifyURLs (false)
Enable to turn relative URLs into absolute.

relativeURLs (false)
Enable this to make all relative URLs relative to content root. Note that this does not affect absolute URLs.

If relativeURLs are turned off by default, wouldn’t canonifyURLs need to be true by default? :thinking:

I don’t think you need those three configuration settings. My website renders relative links and I didn’t set relativeURLs or uglyURLs. And my baseURL is in the format "https://www.example.com/".

I do use only {{ .RelPermalink }} in my theme.

Can you test what the website does with all those additional configuration options removed? :slightly_smiling_face:


Relative URLs also seem to work with the hugo server command.

This is what I get on localhost:

Everything is relative except the two canonical URLs, which should be absolute.