BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Web Development

URL encoding: How percent-encoding works

URL encoding, or percent-encoding, writes a character as % plus two hex digits, so spaces, symbols and non-English letters travel safely in a URL.

Monthly searches · url encoding
110.0k
Keyword research data
url encoding
URL encoding: How percent-encoding works
Click the image to view it full size

A URL can carry only a limited set of characters: letters, digits and a few symbols, each with a fixed job. URL encoding is how everything else gets in. The character is written as its byte value in hexadecimal behind a percent sign: a space becomes %20, an ampersand %26. Letters outside basic English are first turned into their UTF-8 bytes, and each byte gets its own code: é becomes %C3%A9. The standard calls it percent-encoding; most people say URL encoding.

Why URLs need it

A URL uses a few characters as signposts. / separates folders, ? starts the query, & separates parameters, # starts the fragment. If your data contains one of those characters, the browser or server would read it as a signpost.

Say a search form sends the term Jack & Jill:

  • Unencoded: ?q=Jack & Jill looks like a parameter q=Jack followed by a broken second one.
  • Encoded: ?q=Jack%20%26%20Jill keeps the whole phrase as one value.

Which characters to encode

RFC 3986, the standard for URL syntax, sorts characters into groups. RFC 3986

Character Example Encode it?
Unreserved: letters A–Z and a–z, digits, - . _ ~ cold-brew_2 Never needed
Reserved, used as a delimiter the ? that starts a query No, it's doing its job
Reserved, used as data: : / ? # [ ] @ ! $ & ' ( ) * + , ; = an & inside a product name Yes: & becomes %26
The percent sign itself 50% Yes: %25
Space cold brew Yes: %20
Anything outside ASCII ü, 日, emoji Yes, as UTF-8 bytes: ü becomes %C3%BC

Encoding changes meaning only for reserved characters. /a%2Fb (one segment containing a slash) is a different URL from /a/b (two segments). Encoding an unreserved character, such as writing %7E for ~, gives an equivalent URL, but the standard says not to create such codes.

The hex digits can be upper or lower case; %3a and %3A mean the same. The standard asks for uppercase, for consistency.

Space: %20 or +?

Both exist, in different places:

  • In a path or anywhere in a URL, a space is %20. A + in a path is a plain plus sign.
  • In form data, the format HTML forms use (application/x-www-form-urlencoded), a space is written as +. That's why a search page's URL often reads ?q=cold+brew. Servers that parse that format turn + back into a space. WHATWG URL Standard

So if a literal plus sign must survive in a query value, encode it as %2B.

Domain names work differently

Percent-encoding applies to the path, query and fragment. Domain names with non-ASCII letters are converted with Punycode instead: the URL Standard's example turns faß.example into xn--fa-hia.example.

Encoding in code

Use your language's built-in function rather than writing your own. In JavaScript:

  • encodeURIComponent() is for one piece of a URL, such as a query value. It encodes everything except letters, digits and - _ . ! ~ * ' ( ), so &, =, / and ? are all encoded.
  • encodeURI() is for a whole URL. It leaves the signpost characters alone, so don't use it for a single value.

Common mistakes

  • Double encoding. Encoding an already encoded string turns %20 into %2520 (the % itself gets encoded). The standard says never to encode or decode the same string twice. If you see %25 followed by two hex digits where you expected a plain character, something encoded twice.
  • Decoding too early. A server must split a URL into its parts before decoding them, or a decoded %2F turns into a real slash.
  • Mixing forms. Linking to /caf%C3%A9 in one place and /caf%c3%a9 or an unencoded form in another can give crawlers several spellings of one page. Pick one form and use it everywhere: links, sitemap, canonical tags.

URL encoding and SEO

Google follows RFC 3986: reserved characters used as data must be percent-encoded. You can use words in your audience's language in a URL, but Google asks you to percent-encode non-ASCII characters in the href of your links, for example /gem%C3%BCse rather than /gemüse. Google Search Central

For readable URLs, the simplest path is to avoid characters that need encoding in the first place: lowercase letters, digits and hyphens in your URL path, and encoding only where real data calls for it, as in a query string. URL encoding is not the same as character encoding, which describes how a page's text is stored as bytes.

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.