Yesterday I noticed, that my blog did not have the very best urls for my posts than I could have - my last post had the url /post/2018-12-29-Recapping-2018/ and actually I thought it would (and should!) produce something like this: /2018/12/Recapping-2018-90-days-at-Microsoft/ After changing the urls to the correct schema (more in a bit) I noticed that the yearly (e.g. /2018) or monthly (e.g. /2018/12) links do not work at all.
In this post, I show you how I fixed those problems.
Firstly, I changed the links for the posts. My chosen blog engine, Hugo, is so awesome that this is just one setting in the config:
|
|
After that, every “post” will have this link structure /year/month/title.Simple and easy to understand. That change, of course, breaks all the existing links out there and would potentially be a hit on your google SEO and kill all my tweets/posts etc. So I wrote a short PowerShell script to update all my posts to be available at the old address and the new address, in hugo language I added an alias. So far, so good, right? Changing all my posts with PowerShell is awesome, too :)
Yearly / Monthly archives for Hugo
As mentioned in the introduction, I noticed that the yearly (e.g. /2018) and monthly (e.g. /2018/12) urls do not show the expected results - a list of posts from that time. And I am actually a little bit concerned if that is bad for my SEO or not. I didn’t like it, so I wanted to change it. There has to be flag to generate this list, right? Nope, afaik there is no such option yet. The github issue #448 for that feature request gets postponed regularly, so I had only 3 options for it:
- leave it
- learn go and submit a pull request
- find a workaround
So 1. is not acceptable. 2. is unfortunately out of scope right now. So lets find a workaround: After extensive googling I found this workaround that used some javascript/node code to generate the archive pages. I want to stick to the standard, that does not feel right. So I tried another workaround were @onedrawingperday suggested to use a taxonomy to additionally tag the year and month on each post and use taxonomy list pages to show the posts. Not ideal, because the date is already in the frontmatter - but I can live with it. So I changed the site config to have a taxonomy per year:
|
|
Then I created a second powershell to tag all my existing posts with the new date tags in the frontmatter. The month part is a two digit number that is quoted (for sorting, see later in this post!).
|
|
That generates a beautiful, but unfortunately empty page:
Then I figured out that I can create taxonomy list pages to actually show content on those pages. Luckily the fabulous theme my blog uses had two templates that I could copy. Because I created 9 taxonomies, I had to create them for every year:
I modified the taxonomy list template a tiny bit to replace the “02” tag value to “February”. I had to tag it with two digit numeral value to have a proper sorting on the page, here 2019.terms.html (name of the file is important!). I changed the template code to get the tags from .Data.Terms.Alphabetical (line 7) - because the values are two digit numerical, sorting works perfect for this case.
Please note: the files will only work with the theme future imperfect - if you want the same behavior you have to adjust the files to your theme!
|
|
and 2019.html file:
|
|
Then I altered my archetype that I use to create new pages - it should automatically have the taxonomy value so I dont have to take care of it:
|
|
And that results in usable archive pages that fit my needs (see /2018/12 or /2018). A bit inconvenient is, that I have to remember that I need to create a new taxonomy next year and duplicate the taxonomy list template files. Or shall I pre-provision them now? :)
Share this post
Twitter
Facebook
LinkedIn
Email