Use case for relURL and absURL

Searched the forum and didn’t find an answer.

So could someone highlight a use case for relURL and absURL in conjunction with baseURL?

Very much appreciated.

absURL and absLangURL output absolute URLs and the domain root part is added automatically with these functions, so no need to use baseURL in this case.

relURL and relLangURL will output URLs relative to the root of a Hugo project, as long as these URLs do not start with a forward slash / because if such a slash is used then the URL will be made relative to the host root of a domain -not the root of a Hugo project- and in this case if the project is hosted in a subdomain or in a subdirectory then Hugo will not be able to guess that and the URL that begins with the forward slash will throw a 404.

So for reference when making relative URLs it is preferable to enter them in the form of "blog/" and not "/blog/"

In my use case using relURL in the templates is pretty useful so that a Hugo project can be served anywhere despite of what is entered in the baseURL configuration.

I have started using this a lot lately, so that when I am developing a Hugo project I can test it in various devices over a local network.

Of course others may have other use cases for relURL and absURL

8 Likes

Great answer - tnx.

@alexandros is correct. The main motivation is that manually creating URLs with baseURL is hard to do in the templates in a general and robust way. And if, somehow, relURL has some issues, we can fix it in one place and not in 10000000 templates around the internetos.

3 Likes