问题原文
原帖链接 · 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
reservedcharacter 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
