Is it possible to have Dynamic baseURL?

Hi there,

I’ve created a site using Hugo. Everything is OK so far. Now I need to deploy the site to several other domains as a sub-site. To do that, I have to change the baseURL manually everytime.

I’m wondering if there is a way to set a dynamic baseURL, so that I don’t have to change it everytime when deployed to a different domain?

You can set a baseURL by passing the -b flag in the hugo command.

See:

1 Like

There are common scenario where one wants to host the same site with multiple host, say example.org, example.com. given architectural perspective it not necessarily feasible to build different version for each host.

I guess given the static nature of Hugo (not sure there is any runtime js executed the client) one assumes it may not be to possible for dynamic host support based on using window.location.hostname and window.location.protocol to construct dynamic links.

If one has CORS set up with the require allowed origins one can avoid host spoofing.

Is there anything possible in this regard?

If all internal links are site-relative or page-relative, you can host the site under any domain.

1 Like

@jmooring it’s not the site contents but the links built into the generated site in for example the index.html that have the base URL hard coded into them. This will not work in a desirable/consistent way if via another host.

Imagine you have deployment of the same site build to two hosts, one on an internal and one on an external network, there is not way to have these deployment be independent and the links for one will try and fetch resources from the other.

@SimonS I am sorry, but I still do not understand.

Why do you need to hardcode the base URL anywhere on the site?

1 Like

I don’t, if you build the hugo static site, and look at the resultant public/index.htm you notice that any resources, ccc, js, fonts etc have the Base URL generated into the files, as does the public/index.mim<>.js such that deploying the site on a different host word not work.

My suggestion is to. make these links dynamic by leverage the window.location.hostname and window.location.protocol

The base url is configured I the config/next/config.toml or can be overridden on the Hugo command line with the -b option.

That is determined by one or more templates in whichever theme you are using.

You can override the theme’s templates as needed to produce a site where the baseurl never appears in the published HTML.

I’m not sure how to make this any clearer. :slightly_smiling_face:

Im just the DevOps guy know nothing of Hugo/Templates. Pointers to docs would be helpful. With some kind of example ?

Then you might be the wrong person to make the changes.

Documentation…

Main
https://gohugo.io/documentation/

Templates
https://gohugo.io/templates/

Page Variables (compare .RelPermalink to .Permalink)
https://gohugo.io/variables/page/

Functions (compare relURL to absURL)
https://gohugo.io/variables/page/