LoginSignup
2
1

More than 3 years have passed since last update.

hugoでaliasを設定する

Last updated at Posted at 2019-10-11

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などのファイルを確認するかの方法で、判断してください。

2
1
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
2
1