Have date in the filepath, but not in the urls

Hello,

I have a blog where the posts are organized in leaf-bundles.
The paths of the files contain a date (jekyll style):

content/posts/2015-12-03_listening_to_music/index.md
content/posts/2015-12-03_listening_to_music/speaker.jpg

I want the urls to not contain the date, so the resulting post should have the following url:

.../posts/listening_to_music/

I do not really care about the date in the filename.
It is there for me looking at the content/posts directory and seeing my posts in chronological order.
I can set the posts date in the frontmatter, no problem.

I tried to find a way to achieve this, but couldn’t find anything that worked.
I searched for stuff like “strip date from url” or “permalinks jekyll style”, but I wasn’t successful.

Can you please help me remove dates from the urls?

Add this code in your configuration file…

[frontmatter]
  date = [':filename', ':default']

More info Configure Hugo | Hugo.

(NB. I recommend using hyphens over underscores for the URL/filename.)

That is what I had been trying before, but It broke my site. I thought something was wrong with these two lines, but it turns out that I am just not smart enough to use toml right and I made it so my theme wasn’t found anymore.

Thank you a lot, I got it working now.

I have two questions left, though:

  • I see how the above config get’s the date from the path, but I couldn’t find a single sentence in the documentation that tells me that hugo will then remove that date from the url. Am I missing something?
  • Why do you recommend hyphens over underscores? For me as a python programmer the hyphens look more natural.

It is not removing, but extracting the date hugolib: Extract date and slug from filename by bep · Pull Request #4494 · gohugoio/hugo · GitHub

From Google’s URL structure guidelines Google URL Structure Guidelines | Google Search Central  |  Documentation  |  Google for Developers.

Consider using hyphens to separate words in your URLs, as it helps users and search engines identify concepts in the URL more easily. We recommend that you use hyphens (- ) instead of underscores (_ ) in your URLs.

Clarity.

With the browser default for the text-decoration CSS attribute on anchors:

image

vs.

image

1 Like