0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Hugo で記事の URL をカスタマイズ

Last updated at Posted at 2022-03-28

はじめに

  • 静的サイトジェネレータの Hugo では、記事の URL 階層はデフォルトではディレクトリ階層と一致します
  • ですが、場合によっては変更したい場合があります
    • ブログ記事などでは、たとえば /diary/2020/04/19 のような形式にしたい場合もあるでしょう
    • そのためにディレクトリを細かく設定していくのは面倒です
  • その際に、必要な設定について記載します

検証環境

  • hugo v0.93.3

カスタム URL の指定(Permalinks の設定)

  • config.toml[permalinks] 項目を追加することで、記事のパスを自由に設定できます

サンプル

config.toml(抜粋)
[permalinks]
  posts = '/:year/:month/:day/:filename/'
  • 上記の例では、 contents/posts に配置された各記事の URL は 2020/04/19/拡張子を除いた記事のファイル名/ のような形式となります
  • :year のような設定値は他にも用意されているので、必要に応じて公式サイトで確認すると良いでしょう

まとめ

  • URL を物理的なディレクトリ階層と分けられれば、記事の管理がグッと楽になります
  • また、途中でディレクトリ階層を変更しても、記事の URL が変わらないのがありがたいですね
  • なお、この記事は私のブログ記事からの転載です

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?