Skip to main content

Questions ยท Developer Tools ยท 2 min read

When Do I Actually Need URL Encoding?

You need URL encoding whenever text containing reserved characters (?, &, #, /, %, spaces, non-ASCII letters) travels inside a URL, because those characters would otherwise be read as URL structure. Query parameter values almost always need it; already-formed paths mostly do not.

Reserved characters and why they clash

URLs are mini-languages: ? starts a query, & separates parameters, = assigns values, # jumps to fragments, / divides paths. Percent-encoding (turning a character into %XX byte codes) lets data contain these characters as payload without being mistaken for syntax.

The classic bug is a search box: searching "cats & dogs" unencoded yields parameters "cats " and " dogs" because & split the value. Encoded (%26), the ampersand rides along as data. Spaces become %20 (or + in legacy form encoding) for the same reason.

Component versus full-URL encoding

Encoding a component (one value) escapes everything unsafe, including & and =. Encoding a full URL must preserve structural characters, so libraries offer both modes (encodeURIComponent versus encodeURI in JavaScript). Using the wrong mode either breaks the URL or under-protects the data.

Rule of thumb: values get component encoding; assembling components into a URL uses the gentler mode once, at the end. Double-encoding (%2520 appearing where %20 belonged) means something was escaped twice along the way.

Non-ASCII text and international domains

Characters outside ASCII (accented letters, Cyrillic, CJK, emoji) have no direct place in URLs and are UTF-8 encoded then percent-escaped, which is why pasted international links arrive looking like %D0%BF... Modern browsers display them decoded for humans while transmitting the encoded form.

Worth knowing before you start

  • Decode only once and only at the final consumption point; decoding early invites injection bugs.
  • + means space only in query-string form encoding; in paths it is a literal plus. Context decides.
  • Slashes inside a single path segment need escaping (%2F) or routers will treat them as separators.

Frequently asked questions

Do I need to encode an entire existing URL when passing it as a parameter?
Yes: as a parameter value it is data, so its ?, &, and : characters must be component-encoded, or the outer URL will misread its structure as their own.
Why do some links show + for spaces and others %20?
Form-encoded query strings historically used +; percent-encoding is the general standard. Both decode to space in query context, but only %20 is correct in paths.
All questions โ†’
All guides โ†’
All articles โ†’