Using Primary Key / ID Field as an identifier in a URL

前端 未结 6 1295
广开言路
广开言路 2020-12-28 09:56

What are the pros and cons of using your databases primary key as a URL identifier? As an example, http://localhost/post/view/13 - 13 being my primary key for my posts tabl

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 10:33

    As you said, the point of putting titles directly in the URL is SEO. Having keywords in the URL has a significant effect on search engine results.

    However, a few other thoughts related to your examples:

    • I'm not sure why you assume that the reddit alphanumeric key is not the primary, there's nothing that forces primary keys to be numeric. If it's a unique identifier for the post, there's no reason not to use it as the primary key (or at least part of it).
    • Digg actually enforces uniqueness of titles (perhaps just inside a particular category, I haven't been to Digg in years, so I can't recall). I used to see this fairly often with a duplicate story having a URL like:

      http://digg.com/space/Liquid_Water_Recently_Seen_on_Mars_2
      

      This implies that the title is at least part of the primary key, since that's the only way to identify which story the link was intending to be aimed at.

    There isn't really any significant security risk with using the primary key in the URL, other than the ability for people to guess/predict other ones, as pantulis mentioned. But you shouldn't be relying on "nobody will guess this" as a security measure anyway.

提交回复
热议问题