0

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?

GiamPy
  • 3,543
  • 3
  • 30
  • 51

3 Answers3

4

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Love the SO approach. – Alix Axel Mar 10 '14 at 14:07
  • I see. As far as I understood, SO uses the ID to "identify" the question and uses the slug only for SEO purposes, because I tried going to this question removing the question ID and leaving only the question slug, but it didn't work. However, it works using only the ID but without the slug. Does that make sense, @Quentin? – GiamPy Mar 10 '14 at 14:10
  • @GiamPy — Yes, that is what SO does. – Quentin Mar 10 '14 at 14:11
0

When posting the article check if the slug already exists, if it does then append a sequential number to it for instance.

Wordpress prepends a date, but It's also possible to have the same title posted on the same day.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
0

When creating a new post, query your database to see if a post with the same slug already exists. If it does, append a -1 or something similar. If a post already exists with that suffix, try again with -2 and repeat until you have found a unique slug.

Koen
  • 545
  • 3
  • 12