2013-08-10 14:35:34 +00:00
---
2013-08-17 12:34:25 +00:00
aliases:
2014-05-29 22:42:05 +00:00
- /doc/redirects/
- /doc/alias/
- /doc/aliases/
date: 2013-07-09
2014-04-23 07:00:11 +00:00
menu:
main:
2014-05-29 22:42:05 +00:00
parent: extras
next: /extras/builders
prev: /taxonomies/ordering
title: Aliases
weight: 10
2013-08-10 14:35:34 +00:00
---
2015-05-12 06:56:56 +00:00
For people migrating existing published content to Hugo, there's a good chance you need a mechanism to handle redirecting old URLs.
2013-08-10 14:35:34 +00:00
2015-05-12 06:56:56 +00:00
Luckily, redirects can be handled easily with _aliases_ in Hugo.
2013-08-10 14:35:34 +00:00
## Example
2015-05-12 06:56:56 +00:00
2015-08-04 18:00:08 +00:00
Given a post on your current Hugo site, with a path of:
2015-05-12 06:56:56 +00:00
``content/posts/my-awesome-blog-post.md``
2015-08-04 18:00:08 +00:00
... you create an "aliases" section in the frontmatter of your post, and add previous paths to that.
2015-05-12 06:56:56 +00:00
### TOML frontmatter
~~~yaml
+++
...
2015-01-17 07:45:53 +00:00
aliases = [
"/posts/my-original-url/",
2015-05-12 06:56:56 +00:00
"/2010/01/01/even-earlier-url.html"
2015-01-17 07:45:53 +00:00
]
2015-05-12 06:56:56 +00:00
...
2015-01-17 07:45:53 +00:00
+++
2015-05-12 06:56:56 +00:00
~~~
### YAML frontmatter
~~~yaml
---
...
2015-01-28 02:17:09 +00:00
aliases:
- /posts/my-original-url/
2015-05-12 06:56:56 +00:00
- /2010/01/01/even-earlier-url.html
...
2015-01-28 02:17:09 +00:00
---
2015-05-12 06:56:56 +00:00
~~~
2013-08-10 14:35:34 +00:00
2015-08-04 18:00:08 +00:00
Now when you visit any of the locations specified in aliases, _assuming the same site domain_ , you'll be redirected to the page they are specified on.
2013-08-10 14:35:34 +00:00
## Important Behaviors
1. *Hugo makes no assumptions about aliases. They also don't change based
2015-09-09 05:29:04 +00:00
on your UglyURLs setting. You need to provide absolute path to your webroot and the
2013-08-10 14:35:34 +00:00
complete filename or directory.*
2. *Aliases are rendered prior to any content and will be overwritten by
any content with the same location.*
2015-05-12 06:56:56 +00:00
## How Hugo Aliases Work
2015-08-04 18:00:08 +00:00
When aliases are specified, Hugo creates a physical folder structure to match the alias entry, and, an html file specifying the canonical URL for the page, and a redirect target.
2015-05-12 06:56:56 +00:00
2015-08-04 18:00:08 +00:00
Assuming a baseurl of `mysite.tld` , the contents of the html file will look something like:
2015-05-12 06:56:56 +00:00
~~~html
<!DOCTYPE html>
< html >
< head >
< link rel = "canonical" href = "http://mysite.tld/posts/my-original-url" / >
< meta http-equiv = "content-type" content = "text/html; charset=utf-8" / >
< meta http-equiv = "refresh" content = "0;url=http://mysite.tld/posts/my-original-url" / >
< / head >
< / html >
~~~
The `http-equiv="refresh"` line is what performs the redirect, in 0 seconds in this case.