Hugo - Caddy Redirect
If you migrate from wordpress or ghost.
What Is Needed
Redirections For WordPress Migration
# | From | To |
---|---|---|
1 | /index.php/ | / |
2 | /index.php/* | /post/* |
3 | /index.php/category/* | /tags/* |
4 | /index.php/tag/* | /tags/* |
5 | /wp-login.php | / |
Redirections For Ghost Migration
# | From | To |
---|---|---|
6 | /* | /post/* |
7 | /category/* | /tags/* |
8 | /tag/* | /tags/* |
Details
Take #4 /index.php/tag/*
to /tags/*
as example, caddyfile will look like follow:
|
|
Repeat the same for the remaining 7 and start looking ugly.
Factorize
When looking at the requirements, all redirects can be summarized into 2 rules:
Rule # | From | To | Comment |
---|---|---|---|
1 | <some path\> | <some path>/* | Change path |
2 | /<file/dir> | <some path>/* | Move root level items to another path |
Take /index.php/tag/*
to /tags/*
as example, it can be break into 2 steps:
/index.php/tag/*
to/tag/*
/tag/*
to/tags/*
Rule #1 applies to both steps. Creating common rule block in Caddy for Rule #1 and Rule #2 will cover all migration needs.
Caddy Snippets
Caddy v2 support snippet, a parameterized block that works like function call in programming.
Rule #1 Snippets
|
|
This snippet will redirect {args.0}*
, the request url, to {args.1}*
.
It will not execute if request url is /tags*
. This is to prevent redirect loop between /tag
and /tags
.
Again take /index.php/tag/*
to /tags/*
as example:
|
|
Rule #2 Snippets
|
|
This snippet will redirect root level item request, to {args.0}/<item>
, only if {args.0}/<item>/index.html
exist.
Each site can only have one my_tryfile
.
|
|
Final Solution
Following is caddyfile
that covers all 8 redirections mentioned at the beginning:
|
|