Debian Codegraph @

docs/content/en/methods/page/Aliases.md

raw
1 ---
2 title: Aliases
3 description: Returns the aliases defined in front matter as server-relative URLs, resolved according to the current content dimension.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: '[]string'
9 signatures: [PAGE.Aliases]
10 ---
11
12 The `Aliases` method on a `Page` object returns the values defined in the [`aliases`][] front matter field as server-relative URLs, resolved according to the current [content dimension](g).
13
14 The `Aliases` method is useful for generating a `_redirects` file, which contains a source URL, a target URL, and an HTTP status code for each alias. You can use a `_redirects` file with hosting services such as Cloudflare, GitLab Pages, and Netlify.
15
16 ## Redirects
17
18 By default, Hugo handles aliases by creating individual HTML files for each alias path. These files contain a `meta http-equiv="refresh"` tag to redirect the visitor via the browser.
19
20 While functional, generating a single `_redirects` file allows your hosting provider to handle redirects at the server level. This is more efficient than client-side redirection and improves performance by eliminating the need to load a middle-man HTML page.
21
22 > [!TIP]
23 > You can use the same general approach to generate an `.htaccess` file.
24
25 ## Example
26
27 The following example demonstrates how to configure your site and create a template to automate the generation of a `_redirects` file.
28
29 ### Content structure
30
31 The content structure for this multilingual example looks like this:
32
33 ```tree
34 content/
35 ├── examples/
36 │ ├── a.de.md aliases = ['a-old']
37 │ ├── a.en.md aliases = ['a-old', 'a-older']
38 │ ├── b.de.md aliases = ['b-old']
39 │ └── b.en.md aliases = ['b-old', 'b-older']
40 └── _index.md
41 ```
42
43 In the example above, the aliases are [page-relative](g). To specify a [site-relative](g) path, preface the entry with a slash (`/`). Both forms are resolved to [server-relative](g) paths.
44
45 Page-relative paths can also include directory traversal:
46
47 | Path type | File path | Alias | Server-relative path |
48 | :--- | :--- | :--- | :--- |
49 | page-relative | `content/examples/a.en.md` | `a-old` | `/en/examples/a-old/` |
50 | page-relative | `content/examples/a.en.md` | `../a-old` | `/en/a-old/` |
51 | site-relative | `content/examples/a.en.md` | `/a-old` | `/en/a-old/` |
52
53 ### Project configuration
54
55 To implement this, you must update your project configuration to:
56
57 1. Disable the generation of default HTML redirect files by setting `disableAliases` to `true`.
58 1. Define a [media type][] named `text/redirects` to handle the file format.
59 1. Define a custom [output format][] named `redirects` to set the filename to `_redirects` and place it at the root of the published site.
60 1. Configure the home page [outputs][] to include the `redirects` format in addition to `html`.
61
62 {{< code-toggle file=hugo >}}
63 baseURL = 'https://example.org/'
64 disableAliases = true
65
66 defaultContentLanguage = 'en'
67 defaultContentLanguageInSubdir = true
68
69 [languages.en]
70 locale = 'en-US'
71 direction = 'ltr'
72 name = 'English'
73 weight = 1
74 title = 'My Site in English'
75
76 [languages.de]
77 locale = 'de-DE'
78 direction = 'ltr'
79 name = 'Deutsch'
80 weight = 2
81 title = 'My Site in German'
82
83 [mediaTypes]
84 [mediaTypes.'text/redirects']
85 delimiter = ''
86
87 [outputFormats]
88 [outputFormats.redirects]
89 baseName = '_redirects'
90 isPlainText = true
91 mediaType = 'text/redirects'
92 root = true
93
94 [outputs]
95 home = ['html', 'redirects']
96 {{< /code-toggle >}}
97
98 ### Template implementation
99
100 Next, create a home page template specifically for the `redirects` output format. The following template iterates through every page in every language and extracts its aliases.
101
102 To ensure the resulting `_redirects` file is valid, the template uses the [`strings.FindRE`][] function to check for whitespace such as tabs or newlines within the alias string. If whitespace is detected, Hugo will throw an error and fail the build to prevent generating an invalid file.
103
104 ```go-html-template {file="layouts/home.redirects" copy=true}
105 {{- if site.IsDefault -}}
106 {{- range hugo.Sites -}}
107 {{- range $p := .Pages -}}
108 {{- range .Aliases -}}
109 {{- if findRE `\s` . -}}
110 {{- errorf "One of the front matter aliases in %q contains whitespace" $p.String -}}
111 {{- end -}}
112 {{- printf "%s %s 301\n" . $p.RelPermalink -}}
113 {{- end -}}
114 {{- end -}}
115 {{- end -}}
116 {{- end -}}
117 ```
118
119 ### Generated output
120
121 Once Hugo processes the template, it produces a clean list of redirect rules. Each line follows the required format: the source URL, the destination URL, and the HTTP status code.
122
123 The resulting `_redirects` file looks like this:
124
125 ```text
126 /de/examples/a-old /de/examples/a/ 301
127 /de/examples/b-old /de/examples/b/ 301
128 /en/examples/b-old /en/examples/b/ 301
129 /en/examples/b-older /en/examples/b/ 301
130 /en/examples/a-old /en/examples/a/ 301
131 /en/examples/a-older /en/examples/a/ 301
132 ```
133
134 [`aliases`]: /content-management/front-matter/#aliases
135 [`strings.FindRE`]: /functions/strings/findre/
136 [media type]: /configuration/media-types/
137 [output format]: /configuration/output-formats/
138 [outputs]: /configuration/outputs/