パーマリンク設定したい([permalinks] / permalinks.toml)
1# config/_default/hugo.toml
2[permalinks]
3directory_name = "パーマリンクのフォーマット"
[permalinks]セクション、もしくはconfig/_default/permalinks.tomlで、ビルドした記事やページのパーマリンク(URLパターン)を設定できます。
デフォルト(=設定なし)はファイル名をベースとしたURLです。
ファイル名をベースにしたい
1[permalinks]
2post = "/blog/:filename"
3# ==> /blog/記事1/
4
5post = "/:section/:filename"
6# ==> /post/記事1/
ファイル名やパス、ページのタイトルを使ってパーマリンクを定義できます。
ページ(page kind)用のパーマリンク設定に適しています。
:section: セクション名:sections: 階層をもったセクション名。スライス可能:title: ページタイトル。front matterで設定:slug: スラグ。front matterで設定:filename: 拡張子を除外したファイル名:slugorfilename:slugもしくはfilename
公開日をベースにしたい
1[permalinks]
2post = "/:section/:year/:month/:day"
3# ==> /post/2023/10/23/
4
5post = "/:section/:year/:yearday"
6# ==> /post/2023/123
日付トークンを使って、公開日を挿入できます。
記事(post kind)用のパーマリンク設定に適しています。
:year: 年(4桁):month: 月(01 - 12):monthname: 月名:day: 日(01 - 31):weekday: 曜日(0 - 6):weekdayname: 曜日名:yearday: 日数(001 - 365)
個人的に:year/:yeardayを使うのが好きです。
セクションしたい
1[permalinks]
2[permalinks.page]
3posts = "/articles/:year/:yearday"
4tutorials = "/training/:slug"
5# posts/記事/index.md ==> /articles/年/日数/index.html
6# tutorials/記事/index.md ==> /training/スラグ/index.html
7
8[permalinks.section]
9posts = "/articles/"
10tutorials = "/training/"
11# posts/index.md ==> /articles/index.html
12# tutorials/index.md ==> /training/index.html
セクションごとにパーマリンクを設定できます。
タクソノミーしたい
1[permalinks]
2[permalinks.term]
3tags = "/:slug/"
4# ==> /タグ名/
/tags/タグ名/を/タグ名/に変更できます。
多言語したい
パーマリンクは多言語サイトでも同じように設定できます。 実際に次のようなコンテンツ構造のサイトで動作確認しました。
content/
|-- _index.ja.md --> public/ja/index.html
|-- _index.en.md --> public/en/index.html
|-- about.ja.md --> public/ja/about/index.html
|-- about.en.md --> public/en/about/index.html
|-- post/
|-- _index.ja.md --> public/ja/blog/index.html
|-- _index.en.md --> public/en/blog/index.html
|-- 記事1.ja.md --> public/ja/blog/年/月/日/index.html
|-- 記事1.en.md --> public/en/blog/年/月/日/index.html
パーマリンク設計を考える
パーマリンク設定は、HugoだけでなくどのCMSでも構築時に検討が必要な項目です。 固定ページはファイル名をベースとしたパターンで問題ありませんが、 記事ページはファイル名やタイトル名に依存しないパターンが好ましいです。
また、記事ページは作成頻度が高く、また内容が更新される可能性があります。 そのため、記事タイトルに依存するようなパターンや、 URLが重複してりまうようなパターンは好ましくありません。
僕の中では、WordPressのようなデータベースを利用したCMSであれば、 記事ごと一意なページIDが割り振られるので、それを利用するのがパターンがベストだと思います。
Hugoの場合、ページIDは自動で割り振られないので、運用でカバーするしかありません。 同じ日にいくつも記事を作成しないのであれば、公開日ベースのパターンにするのがよいと思います。