Developer Tools6 min read

URL Encoding Explained: A Developer's Complete Guide

What is URL encoding, why it matters and how to use it correctly in web applications. Everything developers need to know about percent-encoding.

P
Published ·Updated 

URLs are the addressing system of the web — every resource has one. But URLs were designed with a limited character set, and the modern web constantly needs to pass data containing characters outside that set. URL encoding, also called percent-encoding, is the solution that makes this possible.

Why URLs Have Character Restrictions

The URL specification (RFC 3986) defines "unreserved characters" safe anywhere in a URL: letters, digits, hyphens, underscores, periods and tildes. All other characters — including spaces, ampersands and equals signs — have special structural meanings or are not guaranteed to be handled correctly by all systems.

How Percent-Encoding Works

Percent-encoding replaces a character with a percent sign followed by the two-digit hexadecimal value of that character in UTF-8. A space (ASCII 32, hex 20) becomes %20. An ampersand (hex 26) becomes %26. For non-ASCII characters, the character is first encoded as UTF-8 bytes and each byte is percent-encoded separately.

encodeURI vs encodeURIComponent

encodeURI() is for encoding a complete URL — it leaves structure characters like : / ? = & unencoded.

encodeURIComponent() is for encoding a single component like a query value — it encodes everything except unreserved characters. The most common mistake is using encodeURI for parameter values containing ampersands or equals signs.

URL Encoding in Query Strings

Query strings use the format key=value&key2=value2. Both keys and values must be encoded. The unencoded query search=JSON & XML parses as two separate parameters. Correctly encoded: search=JSON%20%26%20XML.

URL-Safe Base64

Standard Base64 uses + and / which require encoding in URLs. URL-safe Base64 replaces these with - and _, producing strings safe for URLs without additional encoding — used in JWT tokens and OAuth parameters.

Practical Tips

Always encode query parameter values when constructing URLs programmatically. Never concatenate user input into a URL without encoding. When debugging URL issues, decode the full URL first — the PursTech URL Encoder handles both encoding and decoding with a single click.

❓ Frequently Asked Questions

What is URL encoding and why is it necessary?+
URL encoding converts special characters into a format safe for transmission in a URL. Characters like spaces, ampersands and equals signs have special structural meanings in URLs and must be encoded when used as data values.
What is the difference between encodeURI and encodeURIComponent?+
encodeURI encodes a complete URL, leaving URL structure characters like : / ? = unencoded. encodeURIComponent encodes individual parameter values, encoding everything except unreserved characters. Always use encodeURIComponent for query parameter values.
What does %20 mean in a URL?+
%20 is the percent-encoded representation of a space character. ASCII value 32 in hexadecimal is 20. You may also see + used for spaces in query strings, which is an older encoding convention from HTML forms.
How do I decode a URL-encoded string?+
Use the PursTech URL Encoder — paste any encoded string and click Decode. In JavaScript, use decodeURIComponent() for individual values or decodeURI() for complete URLs.
Is URL encoding the same as HTML encoding?+
No — URL encoding uses % followed by hex digits. HTML encoding uses & followed by a name or number and semicolon. They are different systems designed for different contexts.

💬 Share This Article

📚 Read Next