I just noticed that, on StackOverflow, the URL for a user's profile includes both the numerical ID of the user and the URL-safe version of their username (/users/<user-id>/<username>
)
For example, the appropriate path to my account is /users/10963964/katty-t-enby-cat
.
When I remove the username portion from the URL, it just redirects me, making it as if I had kept the username. However, when I remove the ID, I get a "Page not found." page.
Additionally, when I click on any of the buttons saying "Summary", "Answers", "Tags", etc... it does not change where the URL points to, it just adds a query parameter (tab
) at the end of the URL.
So, my question is: what is the point of putting the username in the URL if /users/<user-id>/
leads to /users/<user-id>/<username>
anyways?
Cheers!
Top comments (3)
It's common to include both a unique ID and a human-readable "slug" (title, name, etc) in URLs. That means the name doesn't need to be unique and can easily be changed without breaking URLs, as long as the ID remains.
One pattern is to use 2 different path segments (i.e. separated by slashes), as used by Stack Overflow; another is to use the same path segment with some delimiter, with the last (or occasionally first) such delimited segment being the ID. Articles on dev.to use the latter pattern:
dev.to/baenencalin/what-is-the-poi...
That last bit
4nac
is an ID of sorts, probably a hashid. Though on dev.to changing the slug manually gives a 404, so maybe the ID is only used to relax uniqueness constraints on titles, not to allow renaming articles to modify the slug.Right, but the name doesn't have to be unique even if the username isn't in the URL. - To me, it just seems like extra characters for no reason; sure, it's human readable, but who's going to be reading that long profile URL as appose to clicking on an anchor bringing them to the URL's destination?
Is there any practical reason not to have just the ID in the URL?
Human-readable URLs are much better for sharing. I can post a slugged URL on any online forum or commenting platform and the reader will have a helpful hint of what the content is before they click on it. If it didn't have a slug, I'd have to decide how to name the link, type out my chosen title, add a hyperlink, etc. and even then, that wouldn't work if the target platform/format didn't allow hyperlinking arbitrary text (e.g. any plaintext format).