Hugo redirect from alias to canonical URL?

Is it possible hugo to produce 301 redirect html pages from aliases in frontmatter
or from a central config file like middleman?
https://middlemanapp.com/basics/redirects/

Not that I know of. But you can do it the other way round:

Rename your file or slug to whatever it should be and set an alias to the old file path.

301 redirects isn’t possible from a static site generator alone (I doubt that Middleman does this).

But you can produce Netlify “_redirects” file and similar to enable this. There are examples around.

2 Likes

As your linked doc says, Middleman does not create 301 redirects but provides an alternative:

However, in some cases, you will not have access to the server to add this feature. Middleman can generate HTML files which will redirect your visitors, but, these old paths are likely to still appear in search engines and will negatively impact your SEO.

The equivalent of that in Hugo is the aliases front-matter parameter:


A _redirect example — Here’s a Netlify _redirect I use for one of my Hugo-generated sites.

Yes, it is not optimal for SEO, but search engines will correct your URLs after some time.

But if you use the way I suggested above just one HTML file with content and the correct file name is created:

All aliases HTML files just contain something like this:

<!doctype html>
<html>
<head>
<title>/test/</title>
<link rel=canonical href=/test/>
<meta name=robots content=noindex>
<meta charset=utf-8>
<meta http-equiv=refresh content="0; url=/test/">
</head>
</html>

So there is no duplicate content. And Hugo uses a canonical tag and noindex for search engines out of the box. Hugo is clever!

Moreover, you can use the canonical tag in your layouts:

<link rel="canonical" href="{{ .Permalink }}">

As you said: A static website generator itself cannot support ‘real’ redirects—also Middleman can’t. If you have a choice you should use e.g. .htaccess or Netlify redirects.

4 Likes