I have a very simple 2 languages website where in both versions I create links
to pdfs. It used to work and it no longer does. I do not understand the reason why.
I have _index.md and _index.en.md for simplicity let’s say they both
contain only:
[blabla](pdf/mypdf.pdf)
The base language part from _index.md works. In the actual html
I read
<a href=pdf/mypdf.pdf>
But the _index.en.md does not work. In the actual html I read the same
<a href=pdf/mypdf.pdf>
and the link goes nowhere. Indeed it tries to open
https://mywebsite.me/en/pdf/mypdf.pdf
whereas the pdf is located at
https://mywebsite.me/pdf/mypdf.pdf
Appart from that I was able to let both versions of the same page link to each other in a menu.
But I don’t know why the links to the pdfs are broken.
Edit: I have no idea how to format code here. 4 spaces does not show the code. Thx cshoredaniel, for the help.
Try adding a forward slash at the beginning like so: [blabla](/pdf/mypdf.pdf)
Rendered URL should resolve to https://mywebsite.me/pdf/mypdf.pdf in both scenarios.
Don’t know how you got this working before, it shouldn’t have worked the way you describe it.
Unless you provide a minimal repo that reproduces the issue in a specific Hugo version.
Bottom line is the following:
In Hugo a URL that begins with a forward slash will always point to the host root.
A URL without a forward slash in the beginning always resolves relative to the PATH.
Therefore what you describe as a problem is in fact proper behavior, even more so in multilingual mode because a user can provide for example a PDF with the same filename under two different languages.
After investigating with a minimal working example, I have found out that my problem actually comes from the baseURL. If I use baseURL = "https://mysite.me/" then no problem everything works.
When I use baseURL = "https://mysite.me/~myself/" then everything is wrong. As you said, the leading / tells the browser to go to the root of the website and the ~myself part is forgotten.
So I guess there should be a standard way for me to still have relative links to my pdfs even if the baseURL is not the root?
I have found the solution. When baseURL is not the root of the website, one needs to add <base href="{{ .Site.BaseURL }}"> to the <head></head> part of the template. Then use no leading /.
I think this should be part of the standard documentation.
It is part of the HTML standard. If you have pdf/pdf.pdf in your link every browser has to assume that the current location is the base. So https://blafasel.com/somewhere/ with a link like that will be calculated into https://blafasel.com/somewhere/pdf/pdf.pdf. You MUST add a slash in the front if you link from site-root.
The base tag in a website makes only sense, if a website is inside of a subfolder. For root based sites it can stay out of the layout.
And baseURL is a parameter that hands your templates a domain name, if you require absolute links. Which you don’t need if you post an internal link.
In the long term relative links (like ../pdf/pdf.pdf) will only lead to more work if you ever change the folder structure. Teach yourself to religiously link to static content via /path/from/siteroot/.