What Is URL Encoding?

A URL can only contain a limited set of characters. Anything outside that set is written as a percent sign followed by two hexadecimal digits, which is what URL encoding, or percent-encoding, does.

What has to be encoded

CharacterEncodedWhy
space%20Not permitted in a URL
&%26Separates query parameters
?%3FStarts the query string
=%3DSeparates key and value
#%23Starts the fragment
/%2FSeparates path segments

Each of these is legal in some positions and meaning-changing in others. A slash inside a query value must be encoded; a slash in the path must not be.

Encode or decode text for a URL.

Open the URL Encoder and Decoder →

How it works

The character is converted to its UTF-8 bytes, and each byte is written as a percent sign and two hexadecimal digits. A space is 0x20, so it becomes %20.

A non-Latin character takes more than one byte. The Arabic letter or the accented e becomes two or three percent sequences, which is why encoded URLs with non-Latin text look so long.

Query strings versus paths

In a path, spaces and slashes are the usual problems. In a query string, the ampersand and equals sign are, because they delimit parameters.

A search for fish & chips sent as q=fish & chips breaks the query: the ampersand ends the parameter. Encoded as q=fish%20%26%20chips, it works.

Plus signs are not spaces everywhere

In form submissions, a plus sign is sometimes used for a space. In a URL path, a plus is a literal plus. That inconsistency causes bugs when a value is encoded for a form and then placed in a path.

Use %20 for a space unless the specific format you are targeting documents plus signs.

Encoding in code

JavaScript has encodeURIComponent for values and encodeURI for whole URLs. The difference matters: encodeURI leaves the delimiters alone, which is right for a complete URL and wrong for a single parameter value.

Python has urllib.parse.quote with a safe argument. Most bugs come from encoding a value twice or from encoding the delimiters along with the value.

Double encoding

Encoding an already-encoded string turns %20 into %2520, because the percent sign itself gets encoded. The receiving system then sees a literal %20 rather than a space.

If a decoded value contains a percent sequence, it was probably encoded twice somewhere in the chain.

Where this shows up

Search queries, redirect URLs passed as parameters, email links, API calls and any place a user-supplied string becomes part of a URL.

It is also the mechanism behind many injection problems: a value that is not encoded can add parameters or change the path entirely.

Common mistakes

Reading an encoded URL

A percent sign followed by 41 is A, 20 is a space, and 2F is a slash. Decoding the string in a tool is faster and less error-prone than reading hex in your head.

When a URL looks unreadable, decode it once. If it becomes readable, it was encoded once, correctly. If it becomes another encoded string, it was encoded twice.

Security note

Encoding is not sanitising. A percent-encoded script tag decodes back to a script tag, so encoding alone does not make user input safe.

Encode for the position the value will occupy, and validate the value itself separately.

A practical rule

Encode each parameter value separately, never the assembled URL. Keep the delimiters you wrote and let the values be escaped.

That single rule removes most URL encoding bugs.

Encoded URLs in logs

Log files are full of percent sequences. Decoding one is often the fastest way to understand what a request actually contained.

If the decoded value contains another percent sequence, the client encoded twice. That is a bug worth fixing at the source rather than at the log.

Frequently asked questions

Is a space %20 or a plus sign?

%20 in a URL path or query. Plus signs are a form-encoding convention.

Do I need to encode a colon or a slash?

Not in the path, where they have structural meaning. Yes inside a query value.

Why did my link break at an ampersand?

The ampersand starts a new parameter. Encode it as %26.

Sources

More free tools

Related guides