The characters that need to be URL encoded are everything except letters, digits and the four marks - . _ ~. In practice, that means spaces, %, quotes, < >, non-ASCII text such as é or emoji, and the structural characters & = ? # / + : whenever they are part of a value instead of the URL's structure. Encoding turns each byte into % plus two hex digits, so a space becomes %20.
Which characters are always safe in a URL?
The always-safe characters are the unreserved set: A-Z, a-z, 0-9, and - . _ ~. The URI standard, RFC 3986, defines that set and says a URL with an unreserved character and the same URL with its encoded form identify the same resource.
So %7E and ~ mean the same thing. The standard also says producers should not create escapes like %41 for A in the first place.
Which characters need to be URL encoded every time?
Some characters need to be URL encoded in every part of a URL, because no part of URL syntax allows them raw.
- Space:
%20. A raw space breaks the URL when it is copied, logged or pasted. - Percent sign:
%25. The standard says a literal%must be encoded, since%marks the start of every escape. - Control characters and all non-ASCII characters. Text is first turned into UTF-8 bytes, and then each byte is escaped.
- Double quote,
<and>:%22,%3C,%3E. Browsers escape these in every URL part. - Backtick,
{ } | \ ^: escaped in paths by browsers, and commonly listed as unsafe for any URL.
Non-ASCII text grows fast. In Node.js v24.15.0, encodeURIComponent("café ✓") returns caf%C3%A9%20%E2%9C%93: the é becomes two bytes and the check mark becomes three.
Reserved characters: encode them only when they are data
Reserved characters are the ones that give a URL its structure, and they need encoding only when they appear inside a value. RFC 3986 lists two groups:
| Group | Characters | What they do |
|---|---|---|
| gen-delims | : / ? # [ ] @ |
Split scheme, host, path, query and fragment |
| sub-delims | ! $ & ' ( ) * + , ; = |
Separate pieces inside a part, like a=1&b=2 |
The standard says producers should percent-encode a reserved character used as data, unless that URL part explicitly allows it as data.
Say you want to search for rock & roll. If you write ?q=rock & roll, the server reads q=rock and then a second, empty parameter named roll. Encode the value and you get ?q=rock%20%26%20roll, which reaches the server as one parameter.
The percent-encoding table gives the escapes you will see most: & is %26, = is %3D, ? is %3F, # is %23, / is %2F, + is %2B and : is %3A.
What to encode in the path, the query and the fragment
What needs encoding depends on which part of the URL a character sits in. The URL Standard, which browsers follow, defines a separate "percent-encode set" for each part. I find this the clearest way to answer the question, because one flat list either encodes too much or too little.
| Where the character goes | Browser escapes when parsing | Encode yourself when it is data |
|---|---|---|
| Path | space " # < > ? ^ { } backtick, non-ASCII |
/ inside a segment, % |
| Query string | space " # < >, non-ASCII (and ' for http and https) |
& = + # and % inside a value |
Fragment (#...) |
space " < > backtick, non-ASCII |
% |
| A single value of any kind | n/a | everything except A-Z a-z 0-9 - _ . ! ~ * ' ( ) |
The last row is what encodeURIComponent() does. The URL Standard says its "component" set gives identical results to that function, so it is the safe choice for any value you insert.
You can see the parser-level escapes for yourself. In Node.js v24.15.0, new URL() turned https://example.com/my file/<x>?q=a b&r="y"#frag ment into this:
https://example.com/my%20file/%3Cx%3E?q=a%20b&r=%22y%22#frag%20ment
The space, <, > and quotes were escaped. The & between parameters was left alone, because the parser can't know whether you meant it as a separator. So the parser alone is not enough: data that contains &, = or # has to be encoded before it goes into the URL.
Space: %20 or a plus sign?
A space is %20 in every part of a URL, and + only in form-encoded query strings. HTML forms and URLSearchParams use the application/x-www-form-urlencoded format, where a space byte becomes + and a literal plus becomes %2B.
new URLSearchParams({ q: "a b&c=d+e" }).toString();
// "q=a+b%26c%3Dd%2Be"
That form format also escapes more than encodeURIComponent() does. Per the URL Standard, it leaves only ASCII letters and digits plus * - . _ unencoded. If your server decodes with the wrong rule, a + that was meant as a plus turns into a space. The fix for that is covered in how to decode plus signs as spaces.
How to URL encode characters in code
The safest way to encode is to use your language's encoder on each value, not to replace characters by hand. In JavaScript you have three tools:
encodeURIComponent(value)for one path segment or one query value.URLSearchParamsto build a whole query string, with+for spaces.new URL()to clean up a full URL that already has the right structure.
This is encodeURIComponent on awkward input, run in Node.js v24.15.0:
encodeURIComponent("a b&c=d/e?f#g%h+i~j!k*l(m)n'o");
// "a%20b%26c%3Dd%2Fe%3Ff%23g%25h%2Bi~j!k*l(m)n'o"
Notice that ~ ! * ( ) ' came through unchanged. The encodeURIComponent reference points out that RFC 3986 reserves ! ' ( ) *, and shows a stricter helper that encodes them too. You only need that for systems that expect strict RFC 3986 output, such as some request-signing schemes.
If you are unsure whether to use encodeURI or encodeURIComponent, read encodeURI vs encodeURIComponent. For a quick check without writing code, paste the value into the URL encoder, which runs in your browser.
Common URL encoding mistakes
Most URL encoding bugs come from encoding the wrong amount or at the wrong time. These are the ones I see most often:
- Double encoding. Encoding
%20again gives%2520. RFC 3986 says implementations must not encode or decode the same string more than once. - A bare percent sign.
decodeURIComponent("100%")throwsURIError: URI malformedin Node.js. The value should have been sent as100%25. - Encoding the whole URL as a value. Running
encodeURIComponentonhttps://example.com/aescapes the:and/, so it stops working as a link. - Commas and pipes in API calls. Some APIs split lists on
,or|, so a comma inside one item should be sent as%2C, and a pipe as%7C. - Mixing up HTML and URL escaping.
&and%26solve different problems. See HTML encoding vs URL encoding.
Lowercase hex such as %2f is not a mistake: the standard treats it as equal to %2F, though it asks producers to emit uppercase.
My take, and one limitation
My rule: never decide character by character. Build URLs with URL and URLSearchParams, or run every inserted value through encodeURIComponent, and leave the structure alone. That handles every character in the tables above without you memorising them.
The limitation is that "correct" encoding depends on who reads the URL. A server, a signing scheme or an old proxy may expect ! and * encoded, + for spaces, or the reverse. When a URL works in the browser but fails at the API, compare the exact bytes each side expects. If you also pass binary data in URLs, URL-safe Base64 avoids most of these characters entirely.
FAQ
Which characters never need URL encoding?
The unreserved characters: A to Z, a to z, 0 to 9, and the four marks - . _ ~. Encoding them is allowed but pointless, because the encoded and plain forms mean the same thing.
Do I need to encode a space in a URL?
Yes. A space is never valid in a URL. Use %20 in paths, query strings and fragments. HTML form submissions use + instead, which only works in the query string.
Does a forward slash need to be URL encoded?
Only when the slash is data. A slash that separates path segments stays as it is. A slash inside one value, such as a file name or a parameter, should become %2F.
Do I need to encode the percent sign?
Yes. A literal % must be written as %25, because % starts every escape sequence. A bare % followed by non-hex characters makes decoders fail.
Should hex digits in percent-encoding be uppercase or lowercase?
Both mean the same thing, but the URI standard says producers should use uppercase, such as %2F rather than %2f.