URL 规范中,问号前面是否要添加斜杠?

2017-09-04 08:55:30 +08:00
 Reign

一个动态的网址,比如: https://www.v2ex.com?page=2 以及 https://www.v2ex.com/?page=2 这两种网址哪个才是最标准规范的 URL 形式?

6780 次点击
所在节点    程序员
33 条回复
yulitian888
2017-09-04 08:58:28 +08:00
scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
jhaohai
2017-09-04 08:59:43 +08:00
如果是根的话需要加的吧
gstqc
2017-09-04 08:59:48 +08:00
第一种不合法
GTim
2017-09-04 09:01:53 +08:00
可以不加
torbrowserbridge
2017-09-04 09:02:07 +08:00
就算不是根,加与不加也是不一样的。例如:/news/?page=1 与 /news?page=1
FanWall
2017-09-04 09:04:32 +08:00
第二种
我觉得第一种的请求成功是因为浏览器自动补齐成第二种(抓包就会发现)
以前遇到类似的,第一种的 url 用 WinhttpCrackUrl 解析的话直接报 invalid
zjsxwc
2017-09-04 09:05:51 +08:00
服务端可以区别,url 是""还是"/"来提供不同的内容。 所以 /something 与 /something/ 是 2 个不同的地址
canbingzt
2017-09-04 09:10:47 +08:00
@torbrowserbridge 确实,java 里可以通过 @RequestMapping("")和 @RequestMapping("/")测试
weyou
2017-09-04 09:11:08 +08:00
其实 2 种都不规范,只是浏览器做了兼容。rfc1738:
HTTP
httpurl = "http://" hostport [ "/" hpath [ "?" search ]]
hpath = hsegment *[ "/" hsegment ] hsegment = *[ uchar | ";" | ":" | "@" | "&" | "=" ] search = *[ uchar | ";" | ":" | "@" | "&" | "=" ]
aleung
2017-09-04 09:23:51 +08:00
@weyou 如果是这样,RFC1738 有局限性了:一定要有 hpath,search 才能存在?
weyou
2017-09-04 09:27:49 +08:00
@aleung rfc 是这样的规定,但是各大浏览器也没有按照这个做,即使没有 hpath 不一样也可以么加 search 么。
aleung
2017-09-04 09:33:13 +08:00
@weyou RFC 1738 is updated by RFC 3968.

```
3. Syntax Components

The generic URI syntax consists of a hierarchical sequence of
components referred to as the scheme, authority, path, query, and
fragment.

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

hier-part = "//" authority path-abempty
/ path-absolute
/ path-rootless
/ path-empty

The scheme and path components are required, though the path may be
empty (no characters). When authority is present, the path must
either be empty or begin with a slash ("/") character. When
authority is not present, the path cannot begin with two slash
characters ("//"). These restrictions result in five different ABNF
rules for a path (Section 3.3), only one of which will match any
given URI reference.

The following are two example URIs and their component parts:

foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
| _____________________|__
/ \ / \
urn:example:animal:ferret:nose

3.3 Path

path = path-abempty ; begins with "/" or is empty
/ path-absolute ; begins with "/" but not "//"
/ path-noscheme ; begins with a non-colon segment
/ path-rootless ; begins with a segment
/ path-empty ; zero characters
```

因此,https://www.v2ex.com?page=2 以及 https://www.v2ex.com/?page=2 这两种网址都符合规范。
aleung
2017-09-04 09:33:49 +08:00
还有,是否支持是看服务端,跟浏览器没有关系。
anyforever
2017-09-04 09:35:25 +08:00
楼主得补点基础知识啊。
aleung
2017-09-04 09:42:30 +08:00
不好意思,输入错了,是 rfc 3986
Mss
2017-09-04 09:47:21 +08:00
不加斜杠请求会出现 301
weyou
2017-09-04 09:49:46 +08:00
@aleung 受教,刚才查的时候没发现还有个更新过的协议。
doubleflower
2017-09-04 10:46:56 +08:00
@Mss 搞笑吧
zzNucker
2017-09-04 10:55:18 +08:00
规范和实现一向差距挺远的
Tink
2017-09-04 11:03:56 +08:00
@Mss 你在逗我

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/387933

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX