1

If an example website contains blog posts created by users this would be an easy way to structure the URLs for each post:

www.example.com/jane-smith/my-first-blog-post

If the user was to change their username (which is unique per user) or the title of their blog post the URL would have to change to be updated. For example if the above user changed her username the URL could be like so:

www.example.com/jane-wilson/my-first-blog-post

A problem now arises as if people try to use the old URL, it would not work. How could this situation be avoided or worked around for an existing website without removing the feature to change usernames or blog post names?

unor
  • 92,415
  • 26
  • 211
  • 360
Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110
  • what happens if two users have exactly same names? – Iłya Bursov Apr 06 '16 at 22:01
  • 1
    You could use a redirect. When a user hits the old URL just redirect them to the new one. However, I would suggest you look into REST services and best REST URL practices; based on your question I think it would help you. – Nick Suwyn Apr 06 '16 at 22:04
  • @Lashane I just edited the question to reflect a better example using usernames instead of names. – Pav Sidhu Apr 06 '16 at 23:05
  • You can track the name changing history of a user, blog post name changing as well. Then, before returning 404 error, try looking to that history and give it a second chance. :D – Huy Tran Apr 06 '16 at 23:35
  • @HuyTran What if someone wanted to use the username? It would become redundant – Pav Sidhu Apr 07 '16 at 00:17
  • @PSidhu so we can combine the username history with the userId, i'm sure that nobody wanna change their userId, hehe. – Huy Tran Apr 07 '16 at 00:59

1 Answers1

0

A common way is to include a unique ID which never changes (for users and posts, in your case), and then 301-redirect to the current/canonical variant.

Example

This is what, for example, Stack Overflow is doing.

If you change your question’s title, the ID (36463097) will stay and the slug will change:

  1. https://stackoverflow.com/questions/36463097/handling-urls-that-change
  2. https://stackoverflow.com/questions/36463097/handling-urls-that-might-change

If you change your user name, the ID (2961662) will stay and the slug will change:

  1. https://stackoverflow.com/users/2961662/psidhu
  2. https://stackoverflow.com/users/2961662/john-doe

This also allows to have users with the same user name (in case you want to allow that):

  • http://example.com/23/john-doe
  • http://example.com/42/john-doe

Drawbacks

It’s not the best URL design:

  • /users/2961662/psidhu is not as beautiful as /users/psidhu
  • remembering a URL with such a cryptic ID is hard(er)
  • such an ID is not meaningful, so ideally it wouldn’t be part of the URL

Alternative

Track all changes and block user names that were once registered. So If I start as john and then change my name to john-doe, no one else may register john.

This is good practice for email addresses, and in my opinion it should also be good practice for every service that allows direct communication with users. If users get messages intended for the previous owner of that user name, that’s a serious problem; if users don’t get their favorite user name because someone else registered it before, it’s, at most, annoying.

So for each user you could track previous user names, and for each post previous titles, and then 301-redirect to the current/canonical one.

Community
  • 1
  • 1
unor
  • 92,415
  • 26
  • 211
  • 360