Permalinks like StackOverflow does them?

Is there a way to create permalinks in Hugo that are similar to how StackOverflow/StackExchange creates for questions?

For example, consider this question’s URL:

http://stackoverflow.com/questions/827823/in-hugo-how-to-create-permalinks-like-stackoverflow

The number 827823 is a unique identifier for the question. The /in-hugo-how-... part is optional and ignored by the web server, however it is included for better SEO.

(Ignored? Yes, try removing it, or altering it. The SO web server ignores that part as long as the number is a valid question number.)

My first attempt is to set the id as the “slug” and then use a permalink format like "/:year/:month/:slug/#:title" but the URLs that are generated omit the # and anything that follows. I used /# instead of # because as a static site, I didn’t want to have to create a rewrite rule on Apache that would ignore anything after the id.

Is this a bug in how permalinks work or am I doing this wrong?

See URL management | Hugo

The spec is clear about what values can be part of a permalink definition, # is not in that spec.

Thanks for the reply. I’m a little confused because I don’t see where in the spec “/” is permitted, but that works. I’m I misreading the page?

Since that avenue is obviously not the right away to go, could you suggest another way to try to achieve the kind of result I’m shooting for?

“/” is the path separator and kind of “goes without saying”. Let’s just say that what you want to do isn’t currently supported, and I have no good suggestions for a workaround, either.

Well, I am a Go programmer, so maybe I’ll take a whack at adding this feature. Do you think that my idea of using /#:title is reasonable?

@TomOnTime What use do you see for having optional parts of a URL? I don’t know a time when I would need it. What if your view?

I don’t understand your question. Do you mean what is the purpose of the optional parts? It is a usability issue. Which link is more usable? myblog.example.com/123 or myblog.example.com/123/employment-statistics-for-2017-january (I hope you think it is the latter). If you look at the URLs for questions on StackOverflow.com, they are formatted that way for a good reason.

Tom

I do agree that the latter is the preferred URL.

What you are asking is to be able to have both of those URLs resolve (or redirect) to the same content. The point of optional parts of a URL is so that you can pass around the shorter URL and have it work just as if the longer has been used.

But I don’t see the need to name content as 123, or why I would ever have someone use the shorter URL when I have the longer URL available.

Does that help clarify my question?

I see two issues here:

  1. Using anchors in URLs (permalink, alias)
  2. Having two paths to a given page.

Issue #2 can be solved with an alias today. However, if you really want it to act like SO where “/123/fragment” would redirect to “/123/fragment-of-my-title”, you’ll need to rely on your web server for that.

Issue #1 isn’t supported today.

1 Like

Would the Hugo owners be open to a PR that adds # support? Is there a technical or other reason why this kind of thing shouldn’t be supported?

I’m hesitant:

  1. An anchored URL should point to something that exists, I don’t see that in your example
  2. The current permalink maps to a physical location on disk, so a “patch” should start by doing some analyse of how involved such a change will be – and sketch up a proposal/design.

But we should first decide if this is something we want. You are the first user asking for it, so unless the hordes are chiming in with a “me want this, too”, I don’t see the greater value in spending time on it.