查询字符串的 URL 部分是问号吗? [英] Is question mark in URL part of query string?

查看:87
本文介绍了查询字符串的 URL 部分是问号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;TR:是查询的 URL 部分中的(第一个)问号还是只是一个分隔符后跟查询?

RFC 1738,第 3.3 节建议?"(问号) 不是查询字符串的一部分,只是将它与路径分开:

http://:/?

RFC 3986 附录 A. 中的语法,也指出?"不是实际查询字符串的一部分:

URI = scheme ":" hier-part [ "?"查询] [#"片段]

现在,让我们考虑两个 URL:

  1. http://server.com/api/item.json
  2. http://server.com/api/item.json?

它们是等效的还是不同的?

区分它们并用于识别两种不同的资源是否有效?

解决方案

tl;dr:

  • ? 不是查询组件的一部分.
  • URI 不等价,因为一个具有没有查询组件,而另一个具有查询组件.
<小时>

URI 标准(STD 66)目前是 RFC 3986.

6.2.比较阶梯 描述了如何测试 URI 是否可能等效的方法.

6.2.3 中.基于方案的规范化它说:

<块引用>

当相关组件为空时,规范化不应删除分隔符,除非方案规范许可这样做.例如,不能假定 URI http://example.com/? 等同于上述任何示例.

以上示例"指的是:

<块引用>

http://example.comhttp://example.com/http://example.com:/http://example.com:80/

(这 4 个 URI 是等效的.)

所以 http://example.com/api/item.json 没有查询组件,而 http://example.com/api/item.json? 有一个空的查询组件.

TL;TR: Is (first) question mark in URL part of query or is is just a separator followed by query?

The RFC 1738, section 3.3, suggests that the "?" (question mark) is not part of the query string, but just separates it from the path:

http://<host>:<port>/<path>?<searchpart>

Grammar presented in the RFC 3986, Appendix A., also indicate the "?" is not part of the actual query string:

URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

Now, let's consider two URLs:

  1. http://server.com/api/item.json
  2. http://server.com/api/item.json?

Are they equivalent or distinct?

Is it valid to distinguish them and use to identify two different resources?

解决方案

tl;dr:

  • The ? is not part of the query component.
  • The URIs are not equivalent, because one has no query component, while the other one has an empty query component.

The URI standard (STD 66) is currently RFC 3986.

Section 6.2. Comparison Ladder describes methods how to test if URIs are possibly equivalent.

In 6.2.3. Scheme-Based Normalization it says:

Normalization should not remove delimiters when their associated component is empty unless licensed to do so by the scheme specification. For example, the URI http://example.com/? cannot be assumed to be equivalent to any of the examples above.

Where "examples above" refers to:

http://example.com
http://example.com/
http://example.com:/
http://example.com:80/

(These 4 URIs are equivalent.)

So http://example.com/api/item.json has no query component, while http://example.com/api/item.json? has an empty query component.

这篇关于查询字符串的 URL 部分是问号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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