How to use aliases across languages in v0.155.0?

Newly in v0.155, Page aliases are language-relative instead of absolute to the web root. Assume that I have got the document my-page.de.md, which will be rendered to /de/my-page.
Now I have defined in the frontmatter of my-page.de.md: aliases = [“/page-on-root”, “/en/my-page”]. Both the aliases newly do not work any more as expected and instead create the two new pages /de/page-on-root as well as /de/en/my-page.

How would I now create the aliases I had before?
I have found that aliases = [“../page-on-root”, “../en/my-page”] works - is this the way to go? It doesn’t feel very intuitive to me.

We made a breaking change in v0.155.0 that was not described in the release notes. I apologize for the oversight, and will amend the release notes later today.

See https://discourse.gohugo.io/t/alias-paths-in-v0-155-0-and-later/56674.


EDIT: I’ve amended the release notes to note the breaking change.

Thanks for the reply, and thanks for the explanation! It only solves half my problems, though. How would I now create an alias to /page-on-root?

With v0.155.0 and later you cannot create an alias to the server root using the aliases feature. Alias paths starting with a slash (/) resolve to the site root, not to the server root.


Perhaps there’s another way to handle your use case; I’ll give it some thought.

This is the setup and objective, as I understand your use case.

baseURL = 'https://example.org/'
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true

[languages.en]
weight = 1
title = "My Site in English"

[languages.de]
weight = 2
title = "Meine Seite auf Deutsch"

content

content/
└── s1/
    └── p1.en.md

desired result

public/
├── alias-to-p1-en/
│   └── index.html
├── de/
│   └── index.html
├── en/
│   ├── s1/
│   │   ├── p1/
│   │   │   └── index.html
│   │   └── index.html
│   └── index.html
└── index.html

Questions:

  1. How many aliases do you need to create in the server root?
  2. Who or what hosts your production site?

Thanks for your effort!

Your MVP looks reasonable to me, though I would also like to have /de/alias-to-p1-en/.

How many aliases do you need to create in the server root?

I would say, about 20. Many of them are links we only keep for backward compatibility and we may just remove them anyways. But there are also some links that are “quick-access” links that we do not want to loose.

Who or what hosts your production site?

We have a pretty basic Debian Server with apache2 (2.4.66) from the official Debian repos.

Since you’re using Apache, assuming mod_alias is active, the most efficient approach is to handle your aliases via server-side redirection. You can have Hugo automatically generate an .htaccess file during the build process to manage this. There’s an example of how to do that with a _redirects file here, which you can modify for the .htaccess format. That example, when modified to create an .htaccess file, should render this:

Redirect 301 "/de/examples/a-old" "/de/examples/a/"
Redirect 301 "/de/examples/b-old" "/de/examples/b/"
Redirect 301 "/en/examples/b-old" "/en/examples/b/"
Redirect 301 "/en/examples/b-older" "/en/examples/b/"
Redirect 301 "/en/examples/a-old" "/en/examples/a/"
Redirect 301 "/en/examples/a-older" "/en/examples/a/"

Then, in the template that produces the .htaccess file, add a block of code to append the “server-relative” redirects defined in a data file.

Try it:

git clone --single-branch -b hugo-forum-topic-56667 https://github.com/jmooring/hugo-testing hugo-forum-topic-56667
cd hugo-forum-topic-56667
rm -rf public/ && hugo && cat public/.htacces

The resulting .htaccess file looks like this:

Redirect 301 "/en/examples/b-old" "/en/examples/b/"
Redirect 301 "/en/examples/b-older" "/en/examples/b/"
Redirect 301 "/en/examples/a-old" "/en/examples/a/"
Redirect 301 "/en/examples/a-older" "/en/examples/a/"
Redirect 301 "/de/examples/b-old" "/de/examples/b/"
Redirect 301 "/de/examples/a-old" "/de/examples/a/"
Redirect 301 "/foo" "/en/examples/a/"
Redirect 301 "/bar" "/de/examples/a/"

The last two are server-relative redirects, allowing you to cross content dimension (role, version, language) boundaries.


If for some reason you don’t want to generate an .htaccess file, you can use the data file approach to generate individual 'meta refresh` HTML files. Let me know if you need an example of this.

This works for me. Thank you very much for your support!

1 Like

You’re welcome. By the way… great question.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.