views:

262

answers:

4

Web pages have moved to use URLs like:

//weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

i.e. they include the title of the page in the url rather than having some coded id.

I understand that this is useful for SEO, and also for users in finding the correct page where they wish to type in the url.

I would like to follow this approach, but wonder how best to acheive it, and particularly how to deal with duplicates.

Is a database trigger which creates the url based on the title and adds a numeric incremental suffix to any duplicates the best way to go, and if so what would such a trigger look like?

+1  A: 

Instead of having an id based on a title they could use id based on both a date and a title (2007/12/03/asp-net-mvc-framework-part-2-url-routing). So if you don't have articles with the same titles in one day (which isn't too severe restriction) duplicates are eliminated.

Alexander Prokofyev
Thanks, good point. Though in my case it is quite possible that two entries could have the same title for the same day.
Richbits
A: 

You've got to model this concept in your application. URL generation based on title can be automatic, but it can't be invisible. WordPress (and probably other CMS's, too) do a pretty good job of this -- they'll default a URL based on the information you enter, but the "key" part of the URL is visible and editable to the user, and uniqueness is enforced at the appropriate level (globally, per month, per day -- whatever).

Making URL generation completely invisible will lead to confusing errors for the user, I believe.

D. Lambert
A: 

In Wordpress at least, the "slug" (as they call it) is generated once from the item's title and stored separately in the database. If two "slugs" collide, it appends -1, -2, etc. to the end. I personally prefer if you add an (optional) field to the submission form to allow people to insert their own—it allows people to specify a shorter URL than my-long-article-is-hard-to-type.

Paul Fisher
I agree. The "slug" is also editable, as long as you don't create a name collision. In any event, the user can see and edit this field, so they've got some control over it if they choose to make changes.
D. Lambert
Thanks, for both your points, I'm not sure that url selection as you describe is that viable in my application, but I'll consider it further. In both scenarios I suppose the code is forming a string and checking if it exists in the database (in the "slug" column for any record), then offering that with or without suffix for confirmation/alteration.
Richbits
A: 

You could do the same thing that SO does. That is, the slug is only there as GoogleJuice. These two URLs resolve to the same thing:

http://stackoverflow.com/questions/925242/asp-net-mvc-route-object-id-title-how-to-deal-with-duplicates

http://stackoverflow.com/questions/925242/

So, in the example you gave, if the CMS gave each post a unique numeric identifier (which I suppose is quite likely) then you can include it in the URL:

http://weblogs.asp.net/scottgu/archive/2007/12/03/1234/asp-net-mvc-framework-part-2-url-routing

In this example, the symbol 1234 is the post's identifier.

Drew Noakes