mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Adding support for aliases (redirects)
This commit is contained in:
parent
45ce6e2b30
commit
d5518c0966
7 changed files with 64 additions and 65 deletions
33
docs/content/doc/aliases.md
Normal file
33
docs/content/doc/aliases.md
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
---
|
||||||
|
title: "Aliases"
|
||||||
|
Pubdate: "2013-07-09"
|
||||||
|
Aliases:
|
||||||
|
- /doc/redirects/
|
||||||
|
- /doc/alias/
|
||||||
|
---
|
||||||
|
|
||||||
|
For people migrating existing published content to Hugo theres a good chance
|
||||||
|
you need a mechanism to handle redirecting old urls.
|
||||||
|
|
||||||
|
Luckily, this can be handled easily with aliases in Hugo.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
**content/posts/my-awesome-blog-post.md**
|
||||||
|
|
||||||
|
---
|
||||||
|
aliases:
|
||||||
|
- /posts/my-original-url/
|
||||||
|
- /2010/even-earlier-url.html
|
||||||
|
---
|
||||||
|
|
||||||
|
Now when you go to any of the aliases locations they
|
||||||
|
will redirect to the page.
|
||||||
|
|
||||||
|
## Important Behaviors
|
||||||
|
|
||||||
|
1. *Hugo makes no assumptions about aliases. They also don't change based
|
||||||
|
on your UglyUrls setting. You Need to provide a relative path and the
|
||||||
|
complete filename or directory.*
|
||||||
|
|
||||||
|
2. *Aliases are rendered prior to any content and will be overwritten by
|
||||||
|
any content with the same location.*
|
|
@ -1,37 +0,0 @@
|
||||||
---
|
|
||||||
title: "Redirects"
|
|
||||||
Pubdate: "2013-07-09"
|
|
||||||
---
|
|
||||||
|
|
||||||
For people migrating existing published content to Hugo theres a good chance
|
|
||||||
you need a mechanism to handle redirecting old urls.
|
|
||||||
|
|
||||||
Luckily, this can be handled easily in a couple of easy steps.
|
|
||||||
|
|
||||||
1. Create a special post for the redirect and mark the file as a `redirect`
|
|
||||||
file in the front matter. Here is an example
|
|
||||||
`content/redirects/my-awesome-blog-post.md` :
|
|
||||||
|
|
||||||
```markdown
|
|
||||||
---
|
|
||||||
redirect: true
|
|
||||||
slug: /my-awesome-blog-post/
|
|
||||||
url: /docs/redirects/
|
|
||||||
---
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Set the redirect template `layouts/redirects/single.html`:
|
|
||||||
|
|
||||||
```html
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<link rel="canonical" href="{{ .Url }}"/>
|
|
||||||
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
||||||
<meta http-equiv="refresh" content="0;url={{ .Url }}" />
|
|
||||||
</head>
|
|
||||||
</html>
|
|
||||||
```
|
|
||||||
|
|
||||||
Now when you go to `/my-awesome-blog-post/` it will do a meta redirect to
|
|
||||||
`/docs/redirects/`.
|
|
|
@ -1,5 +0,0 @@
|
||||||
---
|
|
||||||
redirect: true
|
|
||||||
slug: /my-awesome-blog-post/
|
|
||||||
url: /docs/redirects1/
|
|
||||||
---
|
|
|
@ -19,7 +19,7 @@
|
||||||
<li class="nav-header">Extras</li>
|
<li class="nav-header">Extras</li>
|
||||||
<li> <a href="/doc/shortcodes">ShortCodes</a></li>
|
<li> <a href="/doc/shortcodes">ShortCodes</a></li>
|
||||||
<li> <a href="/doc/indexes">Indexes</a></li>
|
<li> <a href="/doc/indexes">Indexes</a></li>
|
||||||
<li> <a href="/doc/redirects">Redirects</a></li>
|
<li> <a href="/doc/aliases">Aliases</a></li>
|
||||||
<li class="divider"></li>
|
<li class="divider"></li>
|
||||||
<li class="nav-header">Meta</li>
|
<li class="nav-header">Meta</li>
|
||||||
<li> <a href="/doc/release-notes">Release Notes</a></li>
|
<li> <a href="/doc/release-notes">Release Notes</a></li>
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<link rel="canonical" href="{{ .Url }}"/>
|
|
||||||
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
||||||
<meta http-equiv="refresh" content="0;url={{ .Url }}" />
|
|
||||||
</head>
|
|
||||||
</html>
|
|
|
@ -99,21 +99,9 @@ func (p *Page) setSection() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//section := x[len(x)-2]
|
|
||||||
if section := x[len(x)-2]; section != "content" {
|
if section := x[len(x)-2]; section != "content" {
|
||||||
p.Section = section
|
p.Section = section
|
||||||
}
|
}
|
||||||
|
|
||||||
//c := p.Site.Config
|
|
||||||
//systemDirs := map[string]bool{
|
|
||||||
//c.ContentDir: true,
|
|
||||||
//c.StaticDir: true,
|
|
||||||
//c.LayoutDir: true,
|
|
||||||
//}
|
|
||||||
|
|
||||||
//if !systemDirs[section] && !p.Redirect {
|
|
||||||
//p.Section = section
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (page *Page) Type() string {
|
func (page *Page) Type() string {
|
||||||
|
|
|
@ -101,6 +101,7 @@ func (site *Site) Process() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (site *Site) Render() (err error) {
|
func (site *Site) Render() (err error) {
|
||||||
|
site.RenderAliases()
|
||||||
site.ProcessShortcodes()
|
site.ProcessShortcodes()
|
||||||
site.timerStep("render shortcodes")
|
site.timerStep("render shortcodes")
|
||||||
site.AbsUrlify()
|
site.AbsUrlify()
|
||||||
|
@ -144,6 +145,12 @@ func (s *Site) prepTemplates() {
|
||||||
|
|
||||||
templates.Funcs(funcMap)
|
templates.Funcs(funcMap)
|
||||||
|
|
||||||
|
s.Tmpl = templates
|
||||||
|
s.primeTemplates()
|
||||||
|
s.loadTemplates()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Site) loadTemplates() {
|
||||||
walker := func(path string, fi os.FileInfo, err error) error {
|
walker := func(path string, fi os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
PrintErr("Walker: ", err)
|
PrintErr("Walker: ", err)
|
||||||
|
@ -157,15 +164,20 @@ func (s *Site) prepTemplates() {
|
||||||
}
|
}
|
||||||
text := string(filetext)
|
text := string(filetext)
|
||||||
name := path[len(s.Config.GetAbsPath(s.Config.LayoutDir))+1:]
|
name := path[len(s.Config.GetAbsPath(s.Config.LayoutDir))+1:]
|
||||||
t := templates.New(name)
|
t := s.Tmpl.New(name)
|
||||||
template.Must(t.Parse(text))
|
template.Must(t.Parse(text))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filepath.Walk(s.Config.GetAbsPath(s.Config.LayoutDir), walker)
|
filepath.Walk(s.Config.GetAbsPath(s.Config.LayoutDir), walker)
|
||||||
|
}
|
||||||
|
|
||||||
s.Tmpl = templates
|
func (s *Site) primeTemplates() {
|
||||||
|
alias := "<!DOCTYPE html>\n <html>\n <head>\n <link rel=\"canonical\" href=\"{{ . }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
|
||||||
|
|
||||||
|
t := s.Tmpl.New("alias")
|
||||||
|
template.Must(t.Parse(alias))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) initialize() {
|
func (s *Site) initialize() {
|
||||||
|
@ -307,6 +319,22 @@ func (s *Site) BuildSiteMeta() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) RenderAliases() error {
|
||||||
|
for i, p := range s.Pages {
|
||||||
|
for _, a := range p.Aliases {
|
||||||
|
content, err := s.RenderThing(s.Pages[i], "alias")
|
||||||
|
if strings.HasSuffix(a, "/") {
|
||||||
|
a = a + "index.html"
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.WritePublic(a, content.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) RenderPages() error {
|
func (s *Site) RenderPages() error {
|
||||||
for i, _ := range s.Pages {
|
for i, _ := range s.Pages {
|
||||||
content, err := s.RenderThing(s.Pages[i], s.Pages[i].Layout())
|
content, err := s.RenderThing(s.Pages[i], s.Pages[i].Layout())
|
||||||
|
|
Loading…
Reference in a new issue