Skip to content
Developer GuideEncoding

URL Encoding Explained

Learn how URL percent encoding works, which characters are reserved, how spaces and query parameters are encoded, and when to use encodeURIComponent.

What is URL encoding?

Percent encoding lets a URL carry bytes that would otherwise be ambiguous, unsafe, or unavailable in its text form. An encoded byte appears as % followed by two hexadecimal digits, such as %20 for a space.

Encoding must be applied to the correct URL component. A query value, path segment, and complete URL do not share exactly the same safe character set.

Reserved and unreserved characters

Letters, digits, hyphen, period, underscore, and tilde are generally unreserved. Characters such as :, /, ?, #, [, ], and @ help define URL structure. Query syntax also gives special meaning to & and =.

If user data contains a reserved character, encode it as data so it cannot be mistaken for syntax.

Spaces, plus signs, and Unicode

General URI percent encoding represents a space as %20. HTML form query encoding commonly uses + for a space, which means a literal plus sign must be encoded as %2B.

Non-ASCII text is normally converted to UTF-8 bytes before each byte is percent-encoded. Decoder and encoder must agree on the character encoding.

encodeURI versus encodeURIComponent

encodeURI('https://example.test/search?q=hello world');
// https://example.test/search?q=hello%20world

encodeURIComponent('hello world & more');
// hello%20world%20%26%20more

encodeURI leaves URL separators intact. encodeURIComponent is the safer default for an individual dynamic value because it encodes separators that could split or alter that value.

Encoding a query parameter

Value

hello world

Encoded component

hello%20world

Use the URL Encoder / Decoder to encode the value, then append it through a URL API or query builder rather than concatenating untrusted values into a complete URL.

Decode percent-encoded text safely

Decode only the component that was encoded. Repeated decoding can turn intended data into syntax and may create security problems in routers or filters. Treat decoded data as untrusted input and validate it for its final use.

Percent encoding and Base64 encoding solve different transport problems; neither is encryption.

FAQ

Frequently asked questions

What is URL encoding?

URL encoding, or percent encoding, represents bytes that cannot safely appear in a URL component as a percent sign followed by two hexadecimal digits.

Why does a space become %20?

A space byte is hexadecimal 20 in UTF-8 and ASCII, so percent encoding represents it as %20. Form encoding may use a plus sign instead.

What is the difference between encodeURI and encodeURIComponent?

encodeURI preserves URL syntax characters for a mostly complete URL, while encodeURIComponent encodes characters that could change the meaning of an individual query value, path segment, or fragment value.

Should an entire URL be encoded with encodeURIComponent?

Usually no. Encoding an entire URL as one component also encodes separators such as colon, slash, question mark, and ampersand.

Can percent decoding fail?

Yes. Incomplete percent sequences or byte sequences that are not valid for the expected character encoding can cause a decoder to reject the input.

← Back to Developer Guides