Best way to format pretty URLs for numeric IDs

2019-03-10 17:18发布

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.

10条回答
Deceive 欺骗
2楼-- · 2019-03-10 17:39
  1. Doesn't include the title, so you'll lose the additional SEO value of having those keywords in the URL.

  2. Won't work well, because it doesn't have a unique numerical ID, so what are you going to do if someone else tries to post a topic titled "This is a forum post"? Then you start getting into the weird thing digg does, where it has to give the second one the url "http://www.example.com/topic/this-is-a-forum-post_2", and so on. It makes it harder to take the URL they tried to load, and figure out exactly which topic they were trying to get to.

  3. Has the best of both worlds, this would be my style of choice.

查看更多
劫难
3楼-- · 2019-03-10 17:44

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.

查看更多
Viruses.
4楼-- · 2019-03-10 17:49

i would suggest the first one, since the topic title can be changed for clarity, by the admins and then the url will be inconsistent.

www.example.com/topic/123456

also allows one to just edit the last bit of the url (the numbers and jump to another topic), not likely to happen but still a usable feature.

查看更多
放荡不羁爱自由
5楼-- · 2019-03-10 17:50

I'm not convinced longer URL's are SEO trouble. The depth seems to be a bigger issue, and not by counting slashes, but by steps it takes to get from an indexed page with rank to the content page. I recently created a dummy test page titled /content/roofing/how-much-does-a-shingle-roof-cost.html and threw it on the server just to test pathways and make sure my directories were working correctly. I'm not even sure how google discovered the page but it did and it started getting traffic, so I had to give it content and make it part of the family. The dummy content was a copy of our about page so it wasn't empty, but I was surprised an unpromoted page would get traction, and think the URL had something to do with that.

Which brings up a slight alternative to the above 3 choices for a URL. What if you went with number 3 but added .html to the end? I generally do this with dynamic URL's but I have no concrete evidence that it's helpful. According to Google they brag that they can index dynamic URL's just fine and so there's no need to do URL rewrites at all. Google doesn't mind a bit if the other engines aren't as good at that. Several sites I trust add the html at the end (blogger for example) and it can't hurt, so I still do it.

查看更多
登录 后发表回答