Developer Guides

URL Encoding: Fix Spaces, Ampersands and Double Escapes

By MayreDukaan Editorial TeamPublished 4 min read
A query string splits into a rigid separator rail and movable value tiles; an ampersand tile passes through an encoder and emerges as percent-26.
Original AI-generated illustration by MayreDukaan; conceptual, not a product screenshot.

Learn when to encode a URL component rather than an entire address, how percent escapes differ from form-style plus signs, and how to diagnose double encoding without damaging legitimate data.

Separate the address from the value

Most URL-encoding mistakes begin by treating a complete address and one piece of data as the same thing. A URL has structural separators. A query value can contain characters that look like those separators but are intended as ordinary content. Encode the value at the boundary where it enters the URL, not repeatedly at every processing step.

For example, the search phrase tea & coffee contains an ampersand as data. In a query string, an unescaped ampersand can separate parameters. An encoded value such as tea%20%26%20coffee keeps the phrase together. Encoding is about preserving that distinction; it is not encryption, validation or a way to make an unsafe destination trustworthy.

Component encoding versus whole-URI encoding

MDN describes encodeURIComponent as escaping characters using their UTF-8 representation. Unlike encodeURI, it also escapes characters used as URI syntax, including ampersands and equals signs. Neither function should be described as escaping every punctuation character.

TaskAppropriate approachCommon mistake
Add one query valueUse a URL/query builder or component encodingLeave a data ampersand unescaped
Work with an already structured URLParse its parts before changing themEncode the entire string as one value
Nest a URL inside a query valueEncode that nested value once at that layerConfuse the inner and outer separators
Diagnose a malformed linkInspect raw and parsed representationsDecode repeatedly until it looks readable

The URL encoder and decoder has component and full-URI encoding modes. The inspected implementation uses encodeURIComponent and encodeURI respectively. Choose component mode for a single value, not for a complete final address that must retain its separators.

Spaces and plus signs depend on context

Percent encoding commonly represents a space as %20. Form-style query serialization can represent a space as a plus sign. A literal plus must then be distinguished from that space convention, typically through %2B in the encoded value.

Do not replace every plus sign throughout every kind of URL. In particular, a plus in a path is not automatically a form-encoded space. The correct interpretation depends on the component and the serialization convention used by the producer and consumer.

The linked tool's decode action first replaces plus signs with spaces and then calls decodeURIComponent. That is a concrete implementation detail worth knowing: it is convenient for form-like values but is not a lossless general-purpose decoder for arbitrary URL text containing literal plus signs. Preserve the original before experimenting.

Recognize double encoding without guessing

The percent character is encoded as %25. If a value already containing %26 is encoded again, the percent can become %25, producing %2526. That often signals a value passing through the same encoding step twice.

However, %2526 is not automatically a bug. It can be correct when one application deliberately transports the literal text %26, or when an encoded URL is nested inside another encoded value. Trace the layers rather than deleting %25 wherever it appears.

Use text comparison to compare the value before serialization, the resulting URL and the value the server receives. Label each stage. Redact tokens and personal query data before making a debugging sample. Browser address bars and developer tools may display decoded representations, so distinguish display formatting from the actual request.

A repeatable debugging sequence

  1. Identify which substring is the path, query name, query value or nested address.
  2. Recover the original unencoded application value if possible.
  3. Record which component performs encoding and which performs decoding.
  4. Test an ampersand, a literal plus, a space, a percent sign and a non-ASCII character.
  5. Confirm that the receiver reconstructs the intended value after the expected number of decoding steps.

Malformed percent sequences can make decodeURIComponent throw an error. Do not silently discard those characters; an error can reveal damaged input or a disagreement about serialization.

Keep slugs and encoding separate

A URL slug generator is relevant when choosing a human-readable page name, not when preserving an arbitrary query value. Replacing punctuation in a search phrase with hyphens changes the data. Google recommends understandable URL structures, but readable paths do not eliminate the need to serialize user-provided values correctly. Establish a clear data contract first, then choose readable naming conventions for the parts you control.

References

Frequently asked questions

Should I encode an entire URL with encodeURIComponent?

Only when that entire URL is itself a value inside another structure. Otherwise parse and encode the intended components.

Why does an ampersand break a query value?

An unescaped ampersand can be interpreted as a query-parameter separator rather than part of the value.

Is plus always a space?

No. That interpretation belongs to form-style decoding, not every URL component.

What does %2526 mean?

One decoding step produces %26. Whether another step is appropriate depends on the application layers.

Does encoding make a link safe?

No. It preserves representation; it does not validate the destination or prevent every injection problem.

What is special about this decoder?

Its implementation converts plus signs to spaces before applying decodeURIComponent, so retain originals containing literal plus signs.

Useful on MayreDukaan

Sources

Facts last checked against these sources on 17 Nov 2026, 06:40 Pakistan time.

Images: Original AI-generated illustration by MayreDukaan; conceptual, not a product screenshot.

More trending articles