hugo
では、古いURLを新しいURLにリダイレクトする設定を書くことができます。
ただし、これはURLが相対であるため、外部から飛ぶことができません。例えば、以下の設定を書いたとしましょう。
content/img.md
---
title: "img"
aliases:
- /illust
type: img
---
これは、/illustから/imgにURLを変更した内容です。
この場合、例えば、qiitaにURLがリンクされていたとして、URLは以下のようになります。
https://example.com/illust/example.com/img
本来なら、https://example.com/img
にしたいところですが、buildされたHTMLにはプロトコルが指定されずexample.com/img
となってしまうのです。
/illust/index.html
<!DOCTYPE html>
<html>
<head>
<title>img</title>
<link rel="canonical" href="example.com/img/"/>
<meta name="robots" content="noindex">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url=example.com/img/"/>
</head>
</html>
この問題は、layout/alias.html
を設定することで回避できます。
layout/alias.html
<!DOCTYPE html>
<html>
<head>
<title>{{ .Page.Title }}</title>
<link rel="canonical" href="https://{{ .Permalink }}"/>
<meta name="robots" content="noindex">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta http-equiv="refresh" content="0; url=https://{{ .Permalink }}"/>
</head>
</html>
ただし、プレビューではうまく機能しないことがあるので、直接deployするか、build後にpublic/illust/index.htmlなどのファイルを確認するかの方法で、判断してください。