搜索页面的漂亮 URL [英] Pretty URLs for search pages

查看:11
本文介绍了搜索页面的漂亮 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢拥有漂亮"的 URL(例如 /Products/Edit/1 而不是 /products.aspx?productID=1),但我在对于允许您通过大量变量进行搜索的页面,您无法做到这一点.

I really enjoy having "pretty" URLs (e.g. /Products/Edit/1 instead of /products.aspx?productID=1) but I'm at a loss on how to do this for pages that let you search by a large number of variables.

例如,假设您有一个页面,允许用户搜索具有特定名称和特定地址附近的特定类型的所有产品.你会用很长的漂亮"网址来做这个吗

For instance, let's say you have a page that lets a user search for all products of a particular type with a certain name and near a specific address. Would you do this with really long "pretty" URLs

/Products/Search/Type/{producttype}/Name/{name}/Address/{address}

或者只是求助于使用 url 参数

or just resort to using url params

/Products/Search?productType={producttype}&name={name}&address={address}

推荐答案

这个问题主要是关于 URL 设计,只是顺便说一下重写.一旦您将网址设计为,有很多方法可以制作它们工作包括在服务器级别重写或使用基于 URL 的调度的 Web 框架(我认为现在大多数现代 Web 框架都这样做).

This question is primarily about URL design and only incidentally about rewriting. Once you've designed your URLs to be cool, there are lots of ways to make them work including rewriting at the server level or using a web framework that does URL-based dispatch (I think most modern web frameworks do this these days).

美在旁观者的眼中,但我同意您的观点,即很多搜索网址都很丑陋.是什么让他们如此?我认为使 URL 丑陋的主要因素是 URL 中没有添加语义意义的残骸,而是实现细节的结果,如 (.aspx) 或其他扩展名.我的规则是,如果 URL 返回 (X)HTML,则它不应具有扩展名,否则应该具有扩展名.

Beauty is in the eye of the beholder, but I do agree with you that a lot of search urls are ugly. What makes them so? I think the primary thing that makes URLs ugly is cruft in the URL that doesn't add semantic meaning but is the result of an implementation detail, like (.aspx) or other extensions. My rule is that if a URL returns (X)HTML than it shouldn't have an extension, otherwise it ought to.

在搜索的情况下,事实是标准搜索语法确实增加了含义:它表明该页面是一个搜索,它表明参数已命名且可重新排序.丑陋主要来自 ?&= 字符,但实际上您所做的其他任何事情都是将这些相同的字符替换为更有吸引力的字符,例如 |-/,但代价是使 URL 对任何希望解析的软件不透明它就像蜘蛛、缓存代理服务器或其他东西.

In the case of a search, the fact is that the standard search syntax does add meaning: it indicates that the page is a search, it indicates that the arguments are named and reorderable. The ugliness primarily comes from the ?&= characters, but really anything else you do will be to replace these same characters with more attractive characters like |-/, but at the cost of making the URL opaque to any software that wishes to parse it like a spider, a caching proxy server, or something else.

因此请仔细考虑不使用标准语法,并确保您有充分的理由这样做.我认为如果您的参数具有自然顺序并且必须全部定义以使搜索有意义并且紧凑,您可以将其推送到URL中.例如,在博客 URL 中,您可能有:

So think carefully about not using the standard syntax and be sure you have a good reason for doing it. I think in the case where your arguments have a natural order and must all be defined for the search to make sense and are compact, you could push it into the URL. For example, in a blog URL you might have:

/weblog/entries/2008
/weblog/entries/2008/11
/weblog/entries/2008/11/22

分别用于定义 2008 年、2008 年 11 月和 2008 年 11 月 22 日的条目的搜索.您的 URL 应该是唯一且明确的;有时人们会在/-/中输入丢失的搜索参数,我认为这非常紧凑.但是,我会避免将可能很长的参数(例如自由格式的文本查询)推送到 URL 中./weblog/entries/contains/here%20is%20some%20freeform%20text%20blah%20blah 并不比使用查询语法更具吸引力.

For a search defining the entries from 2008, nov 2008, and 22th of november 2008, respectively. Your URLs should be unique and unambiguous; sometimes people put in /-/ for missing search parameters, which I think is pretty compact. However, I would avoid pushing potentially long parameters, like a free-form text query, into the the URL. /weblog/entries/containing/here%20is%20some%20freeform%20text%20blah%20blah is not any more attractive that using the query syntax.

如果您打算使用标准查询语法,那么选择有意义的参数名称​​可能会在某种程度上提高吸引力.products/search?description="blah",虽然更长,但可能比 products/search?q="blah" 更好.在这一点上,我认为是收益递减.

If you are going to use the standard query syntax, then picking argument names that are meaningful might improve the attractiveness, somewhat. products/search?description="blah", though longer, is probably better than products/search?q="blah". At this point it's diminishing returns, I think.

这篇关于搜索页面的漂亮 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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