你会如何设计一个可破解的网址
想象一下,您有一组以良好的树形层次结构组织的产品类别,并且您希望提供可修改的 URL 来浏览这些产品类别。 您可以这样做,
/catalog/categorya/categoryb/categoryc
然后您可以很容易地找出应该列出产品的类别(请注意,需要完整的 URL,因为您可以拥有具有相同名称但位于层次结构中不同位置的类别)
现在会是什么在其中添加产品信息的好方法是什么? 举个例子,您想要显示此类别的产品 Oblivion
/catalog/games/consoles/playstation/adventure
很容易将产品添加到 url 末尾
/catalog/games/consoles/playstation/adventure/oblivion
,但一旦您这样做,您就无法知道其类别或产品是否被称为遗忘。 我个人认为,不被迫添加后缀(例如 .html)
/catalog/games/consoles/playstation/adventure/oblivion.html
将是最好的解决方案,并使用某种前缀,例如
/catalog/games/consoles/playstation/adventure/product:oblivion
您也可以添加某种触发器,例如
/catalog/games/consoles/playstation/adventure/PRODUCT/oblivion
不太好,您会(即使它非常好)不太可能会成为问题)限制自己使用名为产品的类别
到目前为止,后缀解决方案看起来是我从头到尾能想到的最用户友好的方法,但我不喜欢使用扩展程序,
您对此有何看法?
Imagine you had a group of product categories organized in a nice tree hierarchy and you wanted to provide hackable urls to browse these. You could do something like this
/catalog/categorya/categoryb/categoryc
You could then quite easily figure out which category you should list the products for (note that the full URL is needed since you could have categories with the same name but at different locations in the hierarchy)
Now what would be a good approach to add product information in that as well? To give you an example, you wanted to display the product Oblivion for this category
/catalog/games/consoles/playstation/adventure
It's tempting to just add the product at the end of the url
/catalog/games/consoles/playstation/adventure/oblivion
but the moment you do so you loose the ability to know if its category or a product which is called oblivion. I personally feel that not being forced to add a suffix such as .html
/catalog/games/consoles/playstation/adventure/oblivion.html
would be the nicest solution and using some sort of prefix, such as
/catalog/games/consoles/playstation/adventure/product:oblivion
You could also add some sort of trigger like
/catalog/games/consoles/playstation/adventure/PRODUCT/oblivion
not as nice either and you would (even though its very unlikely it would be a problem) restrict yourself from having a category called product
So far a suffix solution looks like the most user-friendly approach that I can think of from the top of my head but I'm not fond of having to use an extension
What are your thoughts on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
深邃的道路让我烦恼。 它们很难分享。
否则,长网址虽然看起来可以破解,但需要您正确获取所有节点元素才能破解它。
以 php.net 为例
,这个模型非常容易被破解,人们一直盲目地使用它。
注意:W3C 认为 .html 后缀在功能上没有意义并且是多余的,应在 URL 中避免使用。
http://www.w3.org/Provider/Style/URI
Deep paths irk me. They're hideous to share.
Otherwise, the long urls, while seeming hackable, require you to get all node elements right to hack it.
Take php.net
And this model is so hackable people use it all the time blindly.
Note: The .html suffix is regarded by the W3C as functionally meaningless and redundant, and should be avoided in URLs.
http://www.w3.org/Provider/Style/URI
让我们剖析您的 URL,使其更加 DRY(不重复)。 这就是您要开始的内容:
实际上,冒险类别是多余的,因为游戏可以属于多种类型。
接下来让我印象深刻的是,游戏机也不需要了。 将 PC 机和控制台机作为一个小部分来区分可能不是一个好主意。 它们都是各种类型的机器,这样做只是增加了另一个层次的复杂性。
现在您需要对您的网站做出一些决定。 我建议删除页面上的
playstation
类别,因为游戏可以跨多个平台存在,而且games
类别也可以存在。 您的网址应如下所示:那么如何获取 Playstation 上所有动作游戏的列表呢?
或者也许
顺序并不重要。 您还必须确保
tags
是产品的保留名称。最后,我假设您由于冲突而无法删除根
/catalog
。 然而,如果您的网站很小并且没有很多其他部分,那么请将所有内容减少到根级别:哦,如果
oblivion
不是一个独特的产品,只需构建一个包含其 ID 的 slug:Lets disect your URL in order to be more DRY (non-repetitive). Here is what you are starting with:
Really, the category
adventure
is redundant as the game can belong to multiple genres.The next thing that strikes me is that consoles is also not needed. It probably isn't a good idea to differentiate between PC's and Console machines as a subsection. They are all types of machines and by doing this you are just adding another level of complexity.
Now you are at the point of making some decisions about your site. I would recommend removing the
playstation
category on your page, as a game can exist across multiple platforms and also thegames
category. Your url should look like:So how do you get a list of all the action games for the Playstation?
or perhaps
The order doesn't really matter. You have to also make sure that
tags
is a reserved name for a product.Lastly, I am assuming that you cannot remove the root
/catalog
due to conflicts. However, if your site is tiny and doesn't have many other sections then reduce everything to the root level:Oh and if
oblivion
isn't a unique product just construct a slug which includes it's ID:这些看起来都很好(除了带冒号的)。
关键是当他们猜错时该怎么办 - 不要将他们发送到 404 - 相反,将你不认识的单词发送到该单词的搜索页面结果 - 如果可以的话那就更好了那里有拼写检查。
Those all look fine (except for the one with the colon).
The key is what to do when they guess wrong -- don't send them to a 404 -- instead, take the words you don't know and send them to your search page results for that word -- even better if you can spell check there.
如果您将不同的部分视为目标,那么产品本身只是另一个目标。
所有目标都应该可以通过 target.html 或仅目标访问。
目录/游戏/控制台/playstation.html
目录/游戏/控制台/playstation
目录/游戏/控制台/playstation/adventure.html
目录/游戏/控制台/playstation/冒险
目录/游戏/控制台/playstation/adventure/oblivion.html
Catalog/games/consoles/playstation/adventure/oblivion
等等以使其保持一致。
我的5分钱...
If you see the different pieces as targets then the product itself is just another target.
All targets should be accessable by target.html or only target.
catalog/games/consoles/playstation.html
catalog/games/consoles/playstation
catalog/games/consoles/playstation/adventure.html
catalog/games/consoles/playstation/adventure
catalog/games/consoles/playstation/adventure/oblivion.html
catalog/games/consoles/playstation/adventure/oblivion
And so on to make it consistent.
My 5 cents...
一个问题是,您的用户对“以良好的树形层次结构组织的一组产品类别”的概念可能与您的概念相符。
以下是 David Weinberger 的 Google 技术演讲“Everything is Miscellaneous”,其中包含一些关于内容分类的有趣想法:
http ://www.youtube.com/watch?v=x3wOhXsjPYM
One problem is that your user's notion of a "group of product categories organized in a nice tree hierarchy" may match yours.
Here's a google tech talk by David Weinberger's "Everything is Miscellaneous" with some interesting ideas on categorizing stuff:
http://www.youtube.com/watch?v=x3wOhXsjPYM
@Lou Franco是的,这两种方法都需要一个强大的后备机制,并将其发送到某种建议页面或搜索引擎将是很好的候选者
@Stefan,将两者视为目标的问题是如何区分它们(就像我所描述的那样)。 最坏的情况是,您首先访问数据库以查看是否有满足该路径的类别,如果不满足,则检查是否有满足该路径的产品。 问题是,对于每个产品路径,您最终都会对数据库进行无用的调用,以确保它不是一个类别。
@some 是的,分隔符可能是一个可能的解决方案,但 .html 后缀更加用户友好且众所周知。
@Lou Franco yeah either method needs a sturdy fallback mechanism and sending it to some sort of suggestion page or seach engine would be good candidates
@Stefan the problem with treating both as targets are how to distinguish them (like I described). At worst case scenario is that you first hit your database to see if there is a category which satisfies the path and if it doesn't then you check if there is a product which does. The problem is that for each product path you will end up making a useless call to the database to make sure its not a category.
@some yeah a delimiter could be a possible solution but then a .html suffix is more userfriendly and commonly known of.
我喜欢 /videogames/consolename/genre/title" 并使用 / 的数量来区分类别或产品。我唯一担心的是多(或难以区分)类型。我强烈建议标题上不要扩展您也可以执行类似 videogames(.php)?c=x360;t=oblivion; 的操作,然后猜测缺少的信息,但是我喜欢 / 方法,因为它看起来更简洁。使用标题的第一个字母或只是做视频游戏/控制台/标题/
i like /videogames/consolename/genre/title" and use the amount of /'s to distinguish between category or product. The only thing i would be worried about multi (or hard to distinguish) genre. I highly recommend no extension on title. You could also do something like videogames(.php)?c=x360;t=oblivion; and just guess the missing information however i like the / method as it looks more neat. Why are you adding genre? it may be easier to use the first letter of the title or just to do videogame/console/title/
我的卑微经验,虽然与销售游戏无关,但告诉我:
通过 ids 更好地设计项目 url,(即 /item/435/ )
标签比类别更好。 也就是说,允许一个项目属于多个类别是比为每个项目分配一个类别更好的方法。
My humble experience, although not related to selling games, tells me:
Better design item urls by ids, (i.e. /item/435/ )
Labels are better than categories. That is to say, allowing an item to belong to several categories is a better approach than assigning one category to each item.
所以呢? 确实没有必要在产品和类别之间进行严格区分,尤其是在 URI 中,除非可能需要考虑额外的数据库调用的性能问题。 如果这对您来说真的很重要,请考虑以下两个建议:
优化重要的地方,例如制作人性化的 URL(如 Chris Lloyd 的回答)。 不要浪费时间试图削减最后可能的百分之一。
So what? There's no real need to make a hard distinction between products and categories, least of all in the URI, except maybe a performance concern over an extra database call. If that's really such a big deal to you, consider these two suggestions:
Optimize where it matters, like making URLs that will be human-friendly (as in Chris Lloyd's answer). Don't waste your time trying to shave off the last possible fraction of a percent.