Permalink - Autogenerate unguessable links to each post placed in a certain directory

Suppose the directory is /content/posts/unguessable. I wished to not show the posts placed in this directory anywhere else (in the main list page /posts, sitemap or rss) and was successful in doing that. For more context, please see this.

At the moment, this is how my [permalinks] looks.

[permalinks]
posts = '/posts/:year/:month/:filename/'

I want it to be something like this.

[permalinks]
posts = '/posts/:year/:month/:filename/'
'posts/unguessable' = 'posts/<some-function-which-gives-a-random-alphabet-string>'

I don’t have a term for it, but it is often used by media platforms (for data purposes, whereas my purpose is to make the url unguessable and nothing more).

Is there a way to achieve this?

Not exactly. Limitations:

  1. Permalinks can be defined for top-level sections only.
  2. Permalink values cannot contain functions.

Maybe something like this would work for you…

content/
├── posts/
│   ├── post-1.md
│   ├── post-2.md
│   └── post-3.md
├── unguessable/
│   ├── _index.md
│   ├── post-4.md
│   └── post-5.md
└── _index.md

content/unguessable/_index.md

+++
title = 'Unguessable'
date = 2021-09-28T21:52:31-07:00
draft = false
[_build]
  list = 'never'
[cascade._build]
  list = 'local'
+++

config.toml

[permalinks]
posts = '/posts/:year/:month/:filename/'
unguessable = '/posts/:050415:yearday/'

where :050415 is the date’s seconds+minutes+hours.

This creates short “unguessable” URLs such as:

/posts/034408276

But, to avoid collisions, each of your “unguessable” posts must have a unique, fully qualified date stamp. For example

date = 2021-10-03T08:44:03-07:00

Wouldn’t it just be easier to give the posts random filenames when you create them?

In a Mac/Linux environment, one way to ensure it was unguessable and unique would be:

hugo new unguessable/$(uuidgen).md

Another approach…

archetypes/posts.md

+++
title = '{{ replace .Name "-" " " | title }}'
date = {{ .Date }}
draft = false
slug = '{{ delimit (shuffle (split (sha1 .Path) "")) "" | first 7 }}'
+++

config.toml

[permalinks]
posts = '/posts/:year/:month/:filename/'
unguessable = '/posts/:slug/'

This creates short “unguessable” URLs such as:

/posts/62c46d1

This will work for posts created day-forward. You would have to add slugs to existing posts.

There’s an open issue to allow something like:

[permalinks]
unguessable = '/posts/:titlemd5/'

But based on the comments I suspect the issue should be closed (rejected).

Is it possible to call .File.UniqueID from the Front Matter?

I guess I’ll go with this.

But a truly random alphanumeric (or atleast hex) sequence with syntax {{ .Random <length> }} which depends on something like /dev/random would make it really expedient.

I’ve tested it, it is possible. It gives the md5 checksum of the path.

1 Like

If you use .File.UniqueID , see this post, some potential issues are detailed and some work-around are suggested

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.