views:

140

answers:

6

I like Stack Overflow's URLs - specifically the forms:

  • /questions/{Id}/{Title}
  • /users/{Id}/{Name}

It's great because as the title of the question changes, the search engines will key in to the new URL but all the old URLs will still work.

Jeff mentioned in one of the podcasts - at the time the Flair feature was being announced - that he regretted some design decisions he had made when it came to these forms. Specifically, he was troubled by his pseudo-verbs, as in:

  1. /users/edit/{Id}
  2. /posts/{Id}/edit

It was a bit unclear which of these verb forms he ended up preferring.

Which pattern do you prefer (1 or 2) and why?

+1  A: 

Neither. Putting a non-English numeric ID in the URL is hardly search engine friendly. You are best to utliize titles with spaces replaced with dashes and all lowercase. So for me the correct form is:

/question/how-do-i-bake-an-apple-pie
/user/frank-krueger
Nissan Fan
This is fine until you realize you can't have two questions with the same title, and that you have to do queries on varchar columns (even indexed) instead of PKs
blu
Um, it's not search engine unfriendly. And your way leads to expensive SQL queries.
Spencer Ruport
The downvoting of this answer is harsh. He is right, those are better URLs! They are impractical to implement however, which is why ids will continue to be used, and exposed to the user. Given that, the way SO does it is probably the best compromise.
rix0rrr
There are plenty of possibilities merging both approaches. This one however simply neglects the merits of IDs which I dislike.
kryoko
Impractical is an overstatement. They're not that difficult and you can easily handle the duplicate problem as well. A properly-index database will be able to handle them without expensive queries as well.
John Sheehan
Why not treat /questions/{title} as a list of all questions with that title? then link to /questions/{id}/{title} so you know how you got there?
Aiden Bell
First, I'm not sure why it's so hard to implement. Second, these are search engine friendly. What can the number 123214 possible tell someone looking at your link/indexing it about the content? Nada. What the URL is can be handled by the program, so simply creating a function to do the conversion of the question to human-readable form and deal with name clashes accordingly is easy.
Nissan Fan
+2  A: 

My guess would be he preferred #2.

If you put the string first it means it always has to be there. Otherwise you get ugly looking urls like:

/users//4534905

No matter what you need the id of the user so this

/user/4534905/

Ends up looking better. If you want fakie verbs you can add them to the end.

/user/4534905/edit
Spencer Ruport
A: 
/question/how-do-i-bake-an-apple-pie
/question/how-do-i-bake-an-apple-pie-2
/question/how-do-i-bake-an-apple-pie-...
JonathanHayward
Someone who is bent on believing that they are correct in saying that the format I suggested is wrong voted you down. Anyone putting PKeys in their URL already has serious issues as a developer. I voted you back upwards.
Nissan Fan
+1  A: 

I like number 2:

also:

/questions/foo == All questions called "foo"
/questions/{id}/foo == A question called "foo"

/users/aiden == All users called aiden
/users/{id}/aiden == A user called aiden
/users/aiden?a=edit or /users/aiden/edit == Edit the list of users called Aiden?
/users/{id}/edit or /users/{id}?a=edit is better

/rss/users/aiden == An RSS update of users called aiden
/rss/users/{id} == An RSS feed of a user's activity
/rss/users/{id}/aiden == An RSS feed of Aiden's profile changes

I don't mind GET arguments personally and think that /x/y/z should refer to a mutable resource and GET/POST/PUT should act upon it.

My 2p

Aiden Bell
I very very disagree >^..^<Questionmarks, ampersands, every sort of special character (besides / ofc) in queries are a crime to the my world.
kryoko
+7  A: 

I prefer pattern 2 for the simple reason is that the URL reads better. Compare:

  1. "I want to access the USERS EDIT resource, for this ID" versus
  2. "I want to access the POSTS resource, with this ID and EDIT it"

If you forget the last part of each URL, then in the second URL you have a nice recovery plan.

  1. Hi, get /users/edit... what? what do you want to edit? Error!
  2. Hi, get /posts/id... oh you want the post with this ID hmm? Cool.

My 2 pennies!

Gav
+1 I like your thinking!
Aiden Bell
+1  A: 

I prefer the 2nd option as well.

But I still believe that the resulting URLs are ugly because there's no meaning whatsoever in there. That's why I tend to split the url creation into two parts:

/posts/42 
/posts/42-goodbye-and-thanks-for-all-the-fish

Both URLs refer to the same document and given the latter only the id is used in the internal query. Thus I can offer somewhat meaningful URLs and still refrain from bloating my Queries.

kryoko
So you're saying that changing from a slash '/' to a hyphen '-' adds meaning? /posts/42/goodbye_and_thanks_for_all_the_fish doesn't look so different from /posts/42_goodbye_and_thanks_for_all_the_fish
Frank Krueger
the signs don't add meaning. The suffix (goodbye and thanks for all the fix) however, does.
kryoko
It's important to note that using another slash in there would be misleading, since we have no nested relation or similar at that point. All we want to do is to give a hint to what content the URL is referring to.
kryoko
I'll conceded that point. But we can also wave our mystic hand and say that the title of the post is a resource of the post itself - and therefore the / is valid. ;-)
Frank Krueger