1

I had URLs like this: example.com/post/dkKE_wd3jJ

For SEO reasons I decided to add some text:

example.com/post/dkKE_wd3jJ/best_funny_videos

But I noticed that the alphanumeric ID looks weird in front, and this order is more user friendly:

example.com/post/best_funny_videos/dkKE_wd3jJ

But now if I go to the parent directory example.com/post/best_funny_videos (for whatever reason, search engine spider indexing the site, etc) the page just does not exist 404 (I could send a 200 response, but that would not be correct, I have no content for that page).

Is this OK?

2

If you are going to use a slash (/) in your URL, you should have content when the URL is truncated to that point. A slash in the URL commonly represents a directory. Both users and bots often shorten URLs to the slash looking for an index page.

An alternative is to use dashes: /post-best_funny_videos-dkKE_wd3jJ. Dashes are not commonly understood to have special meaning in the URL path. A 404 error at a URL truncated to the dash would be perfectly appropriate.

In my experience putting the ID last in the URL leads to problems with long URLs. Long URLs often get truncated by email, forums, and other content management systems. Bots see these truncated URLs and crawl them. When the ID is at the end, the information you need to reconstruct the URL is lost and you have no choice but to return a 404 error. When the ID is near the front, the ID is usually preserved and you can redirect to the correct URL.

I agree that an alpha-numeric ID near the front is not very user friendly. It is hard to read. I'd recommend switching to a numeric ID that is much easier for users to skip over. That way you can still have it near the front.

The other problem with your URLs is your use of underscores to separate words. Underscores in URLs are not good for SEO because Google treats them as part of the word, rather than word separators. In URLs, dashes should always be preferred to underscores to separate words.

Your final URL should:

  • Use dashes rather than slashes so it doesn't look like you have directories.
  • Use dashes rather than underscores to allow Google to parse the URL.
  • Use a numeric ID rather than an alpha-numeric id for usability.
  • Put the ID near the front to allow recovery from URL truncation.

A better URL would be:

/post-23438883-best-funny-videos
2
  • Very good answer. 1. Dashes VS underscores: My bad, I actually use dashes to separate keys, fixed. 2. Numeric ID: I actually come from using numeric IDs, but I wanted to obfuscate it so I don't show how many posts there are, (+ maybe prevent scraping in the future, for now I don't care). And the alphanumeric ID brought me all these "problems", So I'm debating at this point what’s best and if the obfuscation is worth it. – Vixxs Jun 16 '20 at 11:59
  • 1
    Numeric IDs don't have to be sequential. You assign a random ID and check that it isn't already in use; or use a hash of the content. – Stephen Ostermiller Jun 16 '20 at 13:12
1

I can't see anything wrong with serving a 404. After all, there is no content at that URL.

The only other option would be to "redirect" to the correct URL, but by reversing the order of those path segments and having the slug first, that may be tricky, unless the slug is guaranteed to be unique also (in which case you wouldn't need the ID anyway).

An advantage of a URL of the form /post/<id>/<slug> is that you can auto-redirect to </slug> if omitted or incorrect (in much the same way that Stack Exchange does it). And this also allows the slug to change - it triggers a redirect.

3
  • Whether is /post/<id>/<slug> or /post/<slug>/<id> if the slug is incomplete or not there, I fix the URL to add the slug. But there is another problem using /post/<slug>/<id>, some posts don’t have slug, so there could be collisions between slug and id, so I will have to add some distinctive character in front of the slug or id. I confirm the slug is not unique. – Vixxs Jun 16 '20 at 10:50
  • "I fix the URL to add the slug." - Although URLs are more likely to get truncated (shared in emails etc), so if the URL is of the form /post/<slug>/<id> and the last character is missing from the ID then you aren't going to be able to look up the slug (or find the post). On the other hand, if the URL is of the form /post/<id>/<slug> then the URL could be chopped in half and you'd still be able to redirect to canonicalise the URL. If some URLs don't have a slug and your URLs have a fixed number of path segments then you should still be able to determine the <id> from its position. – DocRoot Jun 23 '20 at 22:03
  • Yes, all of that is clear. When I say I fix /post/<slug>/<id> is in the case that the slug for whatever reason is incorrect or inexistent, but obviously is unlikely with this format. Also I saw that Amazon uses this format so I guess is not that bad. I still have not decided, I may do a random looking integer. – Vixxs Jun 24 '20 at 23:57
0

You could use http code 204 "no content"

1
  • It's a possibility, but I believe that google indexes it if somebody links the URL, and I don't like that. – Vixxs Jun 15 '20 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.