URL Encode

Percent-encode text so it survives being put in a URL.

URLs are only allowed to contain a restricted set of characters. Everything else — spaces, ampersands, question marks, accented letters, anything non-Latin — has to be percent-encoded, or the URL breaks in ways that are frequently hard to diagnose.

Why a stray ampersand ruins everything

Certain characters are reserved because they have structural meaning. ? starts the query string, & separates parameters, = splits a key from its value, # begins a fragment.

Put a literal ampersand inside a parameter value — a company name like “Smith & Sons”, say — and the server reads it as the start of a new parameter. Your value gets truncated at “Smith” and a mysterious empty parameter appears. Encoding it as %26 tells the parser it is data, not punctuation.

Encoding a string for a URL

  1. Paste the text you want to put into a URL.
  2. Encode it.
  3. Copy the result into your query string, path segment or redirect target.
  4. Encode each parameter value separately — never encode a whole assembled URL.
  5. Use the decoder to check what an existing encoded URL actually contains.

Characters you will meet most often

CharacterEncodedWhy it matters
space%20 or +Breaks the URL entirely in most contexts
&%26Otherwise read as a parameter separator
?%3FOtherwise read as the start of the query string
=%3DOtherwise read as a key/value separator
#%23Everything after it is treated as a fragment
/%2FOtherwise read as a path separator
+%2BOtherwise may be decoded as a space

Encode the parts, not the whole

The single most common mistake is running a complete URL through an encoder. That turns the :// and the legitimate separators into percent sequences and produces something no server will understand.

Encode each value individually, then assemble the URL around them. In JavaScript that means encodeURIComponent() for values and encodeURI() only if you genuinely need to tidy a whole URL — which you usually do not.

URL encoding questions

What is percent encoding?

A scheme that represents a character as a percent sign followed by its hexadecimal byte value. A space becomes %20 because 20 is the hex code for space in ASCII.

Why do some encoders turn a space into a plus sign?

Because HTML form submissions historically used + for spaces in the query string. Both appear in the wild. %20 is correct everywhere; + is only valid in a query string, never in a path.

When should I use encodeURIComponent instead of encodeURI?

Use encodeURIComponent for individual parameter values, since it escapes the reserved characters too. Use encodeURI only when you have a complete URL and simply need to escape illegal characters like spaces.

How do non-English characters get encoded?

They are converted to UTF-8 bytes first, and each byte is then percent-encoded. That is why a single accented letter often becomes two percent sequences.

Can I encode the same string twice?

You can, and it is a bug waiting to happen. Double encoding turns %20 into %2520, and the server decodes it once to give you a literal %20 in your data.

Cookie
We care about your data and would love to use cookies to improve your experience.