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?

有帮助吗?

解决方案

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.

其他提示

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top