I'm curious if it's okay to use slugs in URLs instead of traditional IDs, especially when the item is unique enough like city names. Are there any best practices or potential pitfalls I should be aware of?
2 Answers
Using slugs in URLs is definitely a good move, as long as they are uniquely and stably assigned. Just remember to manage any edge cases, like duplicate city names or changes in names—nobody wants broken URLs later!
Names of cities can sometimes be misleading since they're not always unique. For instance, I live in a city that's often referred to by a general name rather than its official title. To avoid confusion, I think it's crucial to implement a system where each slug is unique—maybe by including state or country in the slug itself, like 'springfield-illinois-us'.
That approach makes a lot of sense! It's also a good idea to have a unique index for slugs to prevent duplicates.