How to redirect to an article with a slug?

The problem I have is the redirected article does not keep the slug in the URL.

Here’s what happens:

  • Article A has a proper slug, Slug A.
  • Article B should redirect to Slug A using an Alias page, as I’ve created.
  • However, the actual behavior of the Alias during compilation creates an MD file based on Article A’s title and redirects to it instead of Slug A. This then creates two Article As, one with Slug A and one with the file-name-url.

Please advise how to redirect to an article with a slug?

Show the frontmatter of Article A and Article B. There is no such thing like “alias pages” so I think it might not be clear what you really want. To redirect from “article B” to “article A” you do the following in the frontmatter of article A:

---
aliases:
- /url/to/article-b
---

This will lead to url/to/article-b redirecting to article a (whatever its URL is at this point). Of course the slug will change, because it’s a redirect. You don’t need to generate any content file for something that does not exist (article b) and just put it in article a as redirect.

2 Likes

Front Matter

Article A

---
title: "This is Article A's Title, which is long"
slug: "article-a-title"
---

Article B
This is the “old” article that I want to redirect away from to the “new” article above but it generates a url/file that says “this-is-article-as-title-which-is-long” instead of the front matter slug, “article-a-title” and redirects to that url/file instead of Article A’s slug. It completely ignores the URL in the front matter below, if I’m understanding how this works.

---
title: "Article B Title That You're Reading Now"
aliases: ['article-b-title']
url: ['article-a-title']
---

As @davidsneighbour pointed out, you are adding the aliases front matter value to the wrong content file.

1 Like

And you need to either delete content/article-b.md, set its draft field to true, or set the expiryDate field to something in the past.

https://gohugo.io/content-management/urls/#aliases

Hugo renders alias files before rendering pages. A new page with the previous file name will overwrite the alias, as expected.

1 Like

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