URL Encoding Explained: How to Encode and Decode URLs (With Examples)

URL Encoding Explained: How to Encode and Decode URLs (With Examples)

Ever seen a link full of %20, %3A and %2F and wondered what happened to it? That's URL encoding (also called percent-encoding). It's how URLs safely carry spaces, symbols and non-English characters. Understanding it takes five minutes and saves hours of debugging broken links, search forms and API calls.

Why URLs need encoding

URLs may only contain a limited set of characters, and some characters have special jobs:

So if a search term contains &, as in salt & pepper, the browser would think a new parameter starts there. Encoding turns it into %26, which means "a literal ampersand, not a separator".

How percent-encoding works

Each unsafe character is replaced with % followed by its byte value in hexadecimal. Characters outside basic English, such as é or 日, are first converted to UTF-8 bytes, and each byte is encoded: é becomes %C3%A9.

Common codes

CharacterEncodedCharacterEncoded
space%20 (or + in forms)&%26
/%2F=%3D
?%3F#%23
:%3A+%2B
@%40%%25
"%22é%C3%A9

Letters, digits and - _ . ~ never need encoding.

Encode a value, or a full URL?

This is the most common source of bugs:

Value:     https://example.com/a b?x=1
Encoded as a value:  https%3A%2F%2Fexample.com%2Fa%20b%3Fx%3D1
Encoded as a URL:    https://example.com/a%20b?x=1

The first form is what you need when passing a URL as a parameter, for example ?redirect=https%3A%2F%2Fexample.com%2Fdone.

Examples in code

JavaScript

encodeURIComponent('salt & pepper')   // "salt%20%26%20pepper"  (a value)
encodeURI('https://example.com/a b')  // "https://example.com/a%20b"  (a whole URL)
decodeURIComponent('salt%20%26%20pepper') // "salt & pepper"

// Easiest for query strings: let the browser do it
const params = new URLSearchParams({ q: 'salt & pepper', page: 2 });
params.toString()  // "q=salt+%26+pepper&page=2"

PHP

rawurlencode('salt & pepper');   // "salt%20%26%20pepper"
urlencode('salt & pepper');      // "salt+%26+pepper"  (form style)
http_build_query(['q' => 'salt & pepper', 'page' => 2]);

Python

from urllib.parse import quote, urlencode
quote('salt & pepper')                    # 'salt%20%26%20pepper'
urlencode({'q': 'salt & pepper', 'page': 2})  # 'q=salt+%26+pepper&page=2'

Fixing common problems

SymptomCauseFix
%2520 in a URLEncoded twice (% became %25)Encode once, at the point where you build the URL
Parameters cut off at &A value wasn't encodedEncode each value with encodeURIComponent
+ turns into a spaceForm-style decoding treats + as spaceEncode real plus signs as %2B
Strange characters like éUTF-8 decoded as another character setUse UTF-8 everywhere

Encode or decode online

To quickly read a messy link or prepare a value, paste it into the URL encoder and decoder. Choose whether you're encoding a single value or a full URL, and it shows the result instantly in your browser. To inspect each part of a link (protocol, host, path, parameters), use the URL parser.

Encode a single value or a full URL, or decode a messy link back to readable text. Runs in your browser.

Encode or decode a URL

Frequently asked questions

What is URL encoding?

Replacing characters that are not allowed or have special meaning in a URL with a percent sign and two hexadecimal digits. A space becomes %20, an ampersand becomes %26.

Is a space %20 or +?

In the path of a URL, a space is %20. In form data and query strings sent as application/x-www-form-urlencoded, a space is often written as +. Most decoders handle both in query strings.

What is the difference between encoding a value and encoding a full URL?

Encoding a single value (like a search term) converts every special character, including / ? & and :. Encoding a full URL leaves the characters that give the URL its structure alone, so the link still works.

What does %2520 mean?

It is a double-encoded space: %20 was encoded again, turning % into %25. Decode it twice, and fix the code that encodes it twice.

Which JavaScript function should I use?

encodeURIComponent() for individual values such as query parameters, and encodeURI() for a whole URL that is already structured.

Related articles

← All guides