When should a space in a URL be percent-encoded?
Quick answer
Use %20 when a space needs to appear in a URL path or in a query string written as a URL. Use + when the query data is being serialized as application/x-www-form-urlencoded, where + represents a space. The right choice depends on the URL context and serialization convention. Encode the value, not the whole URL indiscriminately, and check how the receiving system interprets it. [S1]
The question, the details, the next step.
A practical checklist
Locate the space: identify whether it is in a path or query value.
Use
%20for a space written in a URL path or URL query.Use
+for a space when the data usesapplication/x-www-form-urlencodedserialization.Encode the relevant value rather than applying a blind replacement across the complete URL.
Check the receiving system’s convention if its interpretation is unclear.
A few more useful answers
Is `+` always the same as `%20` in a URL?
No. + represents a space in application/x-www-form-urlencoded data, while %20 is used for spaces in URLs. Use the form that matches the context. [S1]
What should I do if I do not know how a site expects its query string?
Check the site or tool’s documentation for its serialization convention. The appropriate form depends on the context, and the convention for a particular system may need to be confirmed.
Sources
References used for this guide.
- [S1] Percent-encoding — MDN Glossary developer.mozilla.org · Checked
