John Siu Blog

Tech - Business Tool, Personal Toys

Hugo Front Matter Aliases for Migration

☰ Table of Content

Use Hugo front matter aliases to handle migration from other platform such as Ghost or WordPress.

Background

I have multiple posts about using mod_rewrite / redirect in various web servers to handle difference in base url when migrating from one blog platform to another. Following are a few examples:

FromURLToURLHttp ServerRules
WordPress`/index.php/YYYY/MM/DD/
Hugo/blog/
`
Nginxlocation ~ "^/index.php/\d{4}/\d{2}/\d{2}/(.*)$" { return 301 https://johnsiu.com/blog/$1; }
WordPress`/index.php/
Hugo/blog/
`
Nginxlocation ~ "^/index.php/(.*)$" { return 301 https://johnsiu.com/blog/$1; }
WordPress/index.php/YYYY/MM/DD/<article>WordPress/index.php/<article>Lighttpdurl.redirect = ("^/index.php/\d{4}/\d{2}/\d{2}/(.*)$" => "/index.php/$1")

However I encountered something that can’t be (or too risky) generalized into rules:

FromURLToURL
Ghost/<article>Hugo/blog/<article>

It looks simple by itself but it can be a big mess. The risk is high and I opted for static rules like following:

1
2
3
4
5
6
location /ghost-blog-custom-theme { return 301 https://johnsiu.com/blog/ghost-blog-custom-theme; }
location /ghost-blog-from-wordpress { return 301 https://johnsiu.com/blog/ghost-blog-from-wordpress; }
location /ghost-blog-redirect-for-wordpress { return 301 https://johnsiu.com/blog/ghost-blog-redirect-for-wordpress; }
location /ghost-blog-self-hosted-with-https-using-nodejs-only { return 301 https://johnsiu.com/blog/ghost-blog-self-hosted-with-https-using-nodejs-only; }
location /ghost-https-index-js-in-github { return 301 https://johnsiu.com/blog/ghost-https-index-js-in-github; }
...

It look stupid but safe. I ended up with 43 of them plus other rules!!

Hugo Front Matter Aliases

As I use and explore Hugo more, I found that Hugo front matter support aliases1 and also a tools to extract front matter call front-matter-manipulator.

Script and front-matter-manipulator

I quickly put a script2 together and generated all the aliases I needed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
--- 2017/03/13 ghost-blog-custom-theme.md

aliases:
    - /ghost-blog-custom-theme.md
    - /index.php/ghost-blog-custom-theme.md
    - /index.php/2017/03/13/ghost-blog-custom-theme.md

--- 2017/02/07 ghost-blog-from-wordpress.md

aliases:
    - /ghost-blog-from-wordpress.md
    - /index.php/ghost-blog-from-wordpress.md
    - /index.php/2017/02/07/ghost-blog-from-wordpress.md

--- 2017/02/08 ghost-blog-redirect-for-wordpress.md

aliases:
    - /ghost-blog-redirect-for-wordpress.md
    - /index.php/ghost-blog-redirect-for-wordpress.md
    - /index.php/2017/02/08/ghost-blog-redirect-for-wordpress.md

--- 2017/02/25 ghost-blog-self-hosted-with-https-using-nodejs-only.md

aliases:
    - /ghost-blog-self-hosted-with-https-using-nodejs-only.md
    - /index.php/ghost-blog-self-hosted-with-https-using-nodejs-only.md
    - /index.php/2017/02/25/ghost-blog-self-hosted-with-https-using-nodejs-only.md

--- 2017/03/13 ghost-https-index-js-in-github.md

aliases:
    - /ghost-https-index-js-in-github.md
    - /index.php/ghost-https-index-js-in-github.md
    - /index.php/2017/03/13/ghost-https-index-js-in-github.md

...

I copy & paste them into my post one by one. Only 43 after all. The result is I can removed all web server rules except one.

Pro

The resulting site no longer need web server redirect rules to handle old urls. Especially the 43 rules specifically for migrating from Ghost.

The only remaining rule is for mapping old /tag/<tag> to /tags/<tag>, which is not important.

The site response time is faster as requests don’t have to go through rules. The bigger the site, the more performance is gained(or saved).

The site is 100% static with no dependency on redirect rules!

Con

The public directory became flooded with static directories:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public/
├── a-simple-global-variable-service-for-angular-2/
├── angular-simple-mq-service/
├── angular2-simple-timer-service/
├── apple-touch-icon.png/
├── before-and-after-d/
├── ghost-blog-custom-theme/
├── ghost-blog-from-wordpress/
├── ghost-blog-redirect-for-wordpress/
├── ghost-blog-self-hosted-with-https-using-nodejs-only
├── ghost-https-index-js-in-github/
├── h2ghost/
├── let-ghost-be-ghost/
├── lighttpd-redirect-and-wp-permalink/
├── lighttpd-rewrite-for-wordpress-jetpack/
├── systemd-journal-remote/
...

But those are generated automatically and not my working directory, which is content.

Conclusion

I decided the PRO out weighted the CON in the long run and only a one time effort.

The switch freed my site from web server dependency and can move between different kind of hosting environment with ease.

Reference

John Siu

Update: 2020-08-28
comments powered by Disqus