Best way to format pretty URLs for numeric IDs

2019-03-10 17:01发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

I would think that the 2nd URL would be the best for SEO since it is meaningful and has less depth. It's nicer for people as well since you can look at the URL and know what the content is about.



回答4:

  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.



回答5:

Stackoverflow seems to using pattern 3, with the title being ignored completely (just the id is used).

That makes for nice semantic URL, and is also easy to implement, and still works if the title changes later.

Of course, the title could be completely fake:

Best way to format pretty URLs for numeric IDs



回答6:

I'll go for the first one. You know it really doesn't matter now. Since there are Long URLs converter and it will just proliferate and will become the norm in the future. Remember the longer your URL the less SEO points you'll get.

And you can't control the way people name their forum topics. So really, I'll just choose the first one for simplicity and the norm.



回答7:

For SEO/traffic, definitely no.2 without a doubt. Get those meaningless numbers out of the URL every single time.

www.example.com/topic/this-is-a-forum-post

pickup the "this-is-a-forum-post" from your database and map it back to the ID number within your database via a query. Then do an internal URL re-write to the real page, something like /topic.php?ID=324342



回答8:

I would go with option 2, as SEO can better understand.

Stack Overflow uses the third way, probably, that is the reason, Stack Overflow urls were not optimized for SEO. I am not sure in the above answer.

But In my experience with Google, Quite Often, I could see a solution from other forums, whereas stackoverflow solutions were almost invisible.

Best way to format pretty URLs for numeric IDs Best way to format pretty URLs for numeric IDs

if the both urls were one and the same, the SEO simply goes with option 2, which is less optimized.



回答9:

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.



回答10:

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.