Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a friendly URLs have ID at the beginning or at the end?

Tags:

friendly-url

So I am planning to have friendly URLs for my site where each page has its own ID. So I found two common ways people do this:

http://www.example.com/page/123/slug-for-the-page
http://www.example.com/page/slug-for-the-page/123

Which one is preferred? What are arguments for and against each? I see that when typing the URL the second form is easier, because it is easier for browser to autocomplete from history or bookmarks. But for me it is really strange to have ID at the end.

I will use redirects to canonical URL for any slug which does not match the current one. But does people also do redirects from URL having slug, but no ID, when the slug is unique? For example, redirecting http://www.example.com/page/slug-for-the-page to http://www.example.com/page/slug-for-the-page/123.

like image 638
Mitar Avatar asked Feb 15 '23 19:02

Mitar


1 Answers

First, some history:

The original reason for these URLs was that people wanted their database to be indexed by ID only. Indexing by slug (and enforcing uniqueness) was extra work for both the computer and the programmer. Rails made this style popular (/page/1).

But this style is fairly opaque to search engines. So people added the slug after the ID (/page/1-slug-for-page) to help the search engine (and the user). But the text as the end was irrelevant -- only the number mattered. The slug may not even be stored in the Database (sometimes it's auto-generated), and it wasn't indexed to save space.

Which one is preferred?

For putting the ID in the URL, either style is OK, but the "number then slug" style is much more popular.

I will use redirects to canonical URL for any slug which does not match the current one. But do people also do redirects from URL having slug, but no ID, when the slug is unique?

Just pick ID or slug and run with it.

1) If you lookup by ID, you should ignore the slug (they are only for search engines). You can easily redirect to the 'real' slug if you have the ID. (to correct any truncated URLs, etc.)

2) If you lookup by slug, your slugs need to be unique. (Most sites scope by date to make this easier). Once you're doing a database lookup on the slug, it's not clear why you would even need the ID in the URL. (They look ugly and are quite arbitrary, so it's better to drop them.) What you are proposing is extra work (for you and the computer) but I'm not sure it's useful.

Only high traffic sites need to worry about the index size/speed tradeoffs of indexing on slugs vs IDs. Even then, the difference can be minimized by doing tricks. (i.e. don't lookup the slug, look up the first 8 characters of the MD5 of the slug instead.)

like image 125
BraveNewCurrency Avatar answered May 15 '23 13:05

BraveNewCurrency