网址设计:从网址隐藏pk/id的方法 [英] url design: ways to hide pk/id from url

查看:64
本文介绍了网址设计:从网址隐藏pk/id的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要访问我网站上某个商品的详细信息页面,请使用以下网址

To access the details page of an Item on my site, one would use the following url

<mydomain>/item/1

其中 1 是项目的主键

我正在寻找一种解决方案,可以让我根据以下要求重新设计网址:

I am looking for a solution that allows me to redesign the url with the following requirements:

  • 从网址中排除pk或任何顺序ID
  • 能够唯一访问项目详细信息"页面

我打算将其作为一般的网页设计问题提出,但是我想我应该提到我正在使用Python/Django.

I intended to ask this as a general web design question, but just thought I should mention that I am working with Python/Django.

推荐答案

您需要在URL中使用 some 种标识符,并且该标识符:

You need to have some kind of identifier in the URL, and this identifier:

  1. 必须唯一(两个对象不能具有相同的id)
  2. 必须是永久性的(对象的ID永远不能更改)

因此没有那么多选项,而对象的主键是最佳选择.如果由于某种原因您不能使用它(为什么不使用),则可以对其进行编码或混淆:请参阅此问题,然后它提供了有关如何执行此操作的一些想法的答案.

so there aren't all that many options, and the object's primary key is the best choice. If for some reason you can't use that (why not?) you can encode or obfuscate it: see this question and its answers for some ideas about how to do that.

Stack Overflow自己的URL设计值得一看.您可以通过表格中的任何网址来解决此问题

Stack Overflow's own URL design is worth a look. You can reach this question via any URL of the form

https://stackoverflow.com/questions/9897050/any-text-您喜欢这里!

这允许URL包含问题标题的关键字(对于搜索引擎),同时还可以在标题更改时更改而不破坏旧链接.

This allows the URL to contain keywords from the question's title (for search engines) while also being able to change when the title changes without breaking old links.

这篇关于网址设计:从网址隐藏pk/id的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆