URL 格式:可以 '&'包含在 '?' 之前在网址中? [英] URL formatting: can '&' be included before '?' in the URL?

查看:27
本文介绍了URL 格式:可以 '&'包含在 '?' 之前在网址中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.example.com/&audience=testing?internal-abhishek-jwt=random_string

这是一个有效的网址吗?即是否可以将 &audience 首先和 ?internal-abhishek-jwt 作为第二个查询参数?

请注意受众&作为前缀

解决方案

这是一个有效的网址吗?

URL 规范 表示路径应该按照这些规则:

<块引用>

 path = path-abempty ;以/"开头或者是空的/绝对路径;以/"开头但不是//"/路径方案;以非冒号段开头/无根路径;以段开头/路径空;零个字符path-abempty = *(/"段)路径绝对=/";[段-nz *(/"段)]path-noscheme = 段-nz-nc *(/"段)path-rootless = 段-nz *(/"段)路径空=0 pchar段 = *pchar段-nz = 1*pcharsegment-nz-nc = 1*(未保留/pct编码/子分隔符/@");不带任何冒号:"的非零长度段pchar = unreserved/pct-encoded/sub-delims/:";/"@";

长话短说,允许在段中的最后一行定义的字符类型(这是您要放置 & 的位置).

如果我们查看 sub-delims:

<块引用>

 sub-delims = "!";/$"/&"/'";/"("/")";/*"/+"/,"/;"/="

... 所以 & 是允许的.


也就是说,在查询字符串之外的 URL 中看到 &非常不寻常的.

如果你把一个放在那里,那么转录 URL 的人不太可能会认为你犯了一个错误并更改它......从而破坏它.

所以最好避免使用它们.


<块引用>

即是否可以先将 &audience 和 ?internal-abhishek-jwt 作为第二个查询参数?

没有

? 开始查询字符串.将它们按该顺序排列会使 &audience 成为路径的一部分.

https://www.example.com/&audience=testingting?internal-abhishek-jwt=random_string

Is this a valid URL? i.e is it fine to have &audience first and ?internal-abhishek-jwt as the second query paramter?

Please note audience has & as prefix

解决方案

Is this a valid URL?

The URL spec says that the path should be expressed per these rules:

  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

  path-abempty  = *( "/" segment )
  path-absolute = "/" [ segment-nz *( "/" segment ) ]
  path-noscheme = segment-nz-nc *( "/" segment )
  path-rootless = segment-nz *( "/" segment )
  path-empty    = 0<pchar>
  segment       = *pchar
  segment-nz    = 1*pchar
  segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
                ; non-zero-length segment without any colon ":"

  pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"

Which, to cut a long story short, allows the types of character defined on the last line in a segment (which is where you want to put your &).

And if we look at sub-delims:

 sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
             / "*" / "+" / "," / ";" / "="

… so & is allowed.


That said, it is highly unusual to see a & in a URL outside of a query string.

If you put one there then it is not unlikely that someone transcribing the URL will assume you made a mistake and change it … thus breaking it.

So they are best avoided.


i.e is it fine to have &audience first and ?internal-abhishek-jwt as the second query paramter?

No.

The ? starts the query string. Putting them in that order makes &audience part of the path.

这篇关于URL 格式:可以 '&amp;'包含在 '?' 之前在网址中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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