Search code examples
phppretty-urls

Pretty URLs and resource items with the same name


I have a question for today.

If I am developing, for example, a blog that uses pretty URLs to link the blog posts like

/posts/welcome-to-my-blog

And sometimes later I created another blog post with the same name, therefore the link would be

/posts/welcome-to-my-blog

I would have two blog posts with the same name, same URLs but.. different posts, different content, different purposes.

I know this is an unlikely example, but how could this be solved?


Solution

  • You can either go with the Stackoverflow approach and include a unique identifier as well as a human friendly name:

    /posts/13523/welcome-to-my-blog
    /posts/83841/welcome-to-my-blog
    

    Or you can enforce uniqueness on your slugs and set up your CMS to throw an error (or add an automatic modification such as a numerical suffix) if a user tries to create a duplicate.