大网数据广告活动
网站运营与故障问答

Usage of comma in URL: encoded or not encoded

· 52CE 我爱测

Even have seen a page, where in the same url were both of enocoded and not encoded commas, like: https://example.com/product?filter_color:blue,green&filter_size:xl%2Cxxl My knowledge about the subject is a bit messy: On the one s…

问题原文

原帖链接 · Evgeniy · CC BY-SA 3.0

Even have seen a page, where in the same url were both of enocoded and not encoded commas, like: https://example.com/product?filter_color:blue,green&filter_size:xl%2Cxxl

My knowledge about the subject is a bit messy:

  • On the one side, in a URI RFC i've read, comma would be a socalled reserved character and should be in URLs always encoded.
  • On the other side i've seen many websites, where comma wasn't encoded.

The question arose because of usage of both encoded and non encoded variant: how should it be done correctly: encode or non encode?

已采纳解答

原帖链接 · unor · CC BY-SA 3.0

, is a reserved character. Reserved characters are never equivalent (for normalization purposes) to their percent-encoded variants. So these URIs are not equivalent:

http://example.com/?foo,bar
http://example.com/?foo%2Cbar

Neither the URI standard¹ nor the HTTP/HTTPS URI scheme specs define a special role for , in the query component. This means that authors may use , to represent data in the query component (i.e., for whatever they want).

It can make sense to use , together with %2C in an URI’s query component. For example, an author could decide to use , for separating name-value pairs, and %2C for representing commas within values:

http://example.com/?score:1%2C4,time:55

(It doesn’t seem to make sense in the example URI in your question, though. Assuming that the values are "blue" and "green", as well as "xl" and "xxl", it would make more sense to either use , or %2C in both cases. Your example URI would make sense if e.g. the latter case is actually one value, so "xl,xxl".)


¹ Note that RFC 2396 is obsolete. IETF’s URI standard should always be accessible under STD 66, which is currently RFC 3986.

I gave a similar answer to the question Possible side effect using comma in querystring? on Stack Overflow.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。