Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to format pretty URLs for numeric IDs

Alright, so let's say I'm writing a forum application, and I want pretty URLs. However, all my tables use numeric IDs, so I'm not sure the best way to format the URLs for those resources. Let's pretend I'm trying to get a topic with ID 123456 and title This is a forum post. I've seen it done a couple ways:

  1. www.example.com/topic/123456
  2. www.example.com/topic/this-is-a-forum-post
  3. www.example.com/topic/123456/this-is-a-forum-post

Which one would you say is, taking all things into consideration (including SEO), the optimal URL?

Sorry if this question is too vague, but it seems programming-related and it's not incredibly open-ended, as I just want to hear the pros and cons of each method.

like image 562
Sasha Chedygov Avatar asked May 12 '09 04:05

Sasha Chedygov


2 Answers

I would go with option 3, and make the slug (the last bit) optional

Because?

  • The ID will always be unique... 2 people may make a thread with the name 'good news' for example
  • The search bots can access the slug for some SEO goodness
  • The slug should be optional ... Using just the ID should still give you access to the site. Perhaps if the slug isn't there you could forward to the slug'd version, if you're concerned about duplicate content. You could always use the canonical meta tag to tell Google to index the slugged version.
  • Another benefit of the optional slug is if someone copies and pastes the URL into a document, there is a chance it could have characters at the end chopped off (because URLs generally don't have spaces, so they don't break to new lines). Having the slug optional means there is more of a chance people will find your page.

I believe this is what Stack Overflow does.. and also notice they are doing rather well in the Search Engines.

Update

From the comments, be sure to 301 redirect any missing slug version to the correct slug.

like image 95
alex Avatar answered Sep 29 '22 01:09

alex


URL 1 is definitely suboptimal. URL 2 is attractive but you run the risk of confusion if tags collide, especially if they differ only in punctuation. So I'd say URL 3 is the clear winner.

Also note that just because you display URL 3 is no reason not to accept all 3, with the other two redirecting. If URL 2 is ambiguous, it should redirect to a disambiguation page.

like image 29
Norman Ramsey Avatar answered Sep 28 '22 23:09

Norman Ramsey