URL TEXT

Keeping an ampersand inside one query-field value

An ampersand or equals sign inside a field value has to be encoded before the value is placed in a query string. This guide encodes a title containing both and explains why the whole query must not be encoded as one piece.

Delimiters have jobs in a query string

A query string is a list of name and value pairs. An equals sign separates a name from its value, an ampersand separates one pair from the next, and a question mark starts the list. A hash character starts the fragment, which is not sent to the server. When data contains one of these characters, a parser cannot tell whether it is part of the data or part of the structure. A title field with the value red & blue would be read as the value red followed by a new, nameless pair blue. Encoding the value before inserting it removes the ambiguity, because the encoded form contains no structural characters.

Encoding the title

The first worked case encodes the text red & blue and obtains red%20%26%20blue. The spaces become %20 and the ampersand becomes %26, the hexadecimal for its byte value 38. Inserted into a query, the result reads title=red%20%26%20blue&page=2, and a parser sees two pairs, title and page, with the first value intact. Decoding the component restores red & blue, as the third worked case shows. The round trip preserves the original characters, which is the property that matters: what the sender wrote is what the receiver reads after one decoding pass.

The equals sign inside data

The second worked case encodes a=b and obtains a%3Db. A raw equals sign in a value usually survives parsing because most parsers split a pair at the first equals sign only, but relying on that is risky. Some libraries split on every equals sign, and the pair x=a=b may be read as three tokens or rejected. Encoding the data character as %3D removes the dependence on parser details. The same advice covers semicolons, which some older systems treated as pair separators, and the hash sign, which must be %23 inside a value or the rest of the text will be dropped as a fragment.

Encode each value, then assemble

The order of operations matters. Encode each name and each value separately, then join them with the equals signs and ampersands that belong to the structure. For the title and a page number, the pieces are title and red%20%26%20blue, joined as title=red%20%26%20blue, followed by an ampersand and page=2. Building the query this way keeps structure and data apart. A common shortcut in code is to concatenate raw strings and encode the result, which fails whenever a value contains a delimiter. Many languages provide helpers that build a query from a dictionary and handle the encoding of each part. When you write the string by hand, for a test or a documentation example, the same discipline applies: type the encoded value first, then add the separators. Reading the finished string aloud, pair by pair, is a quick way to confirm that exactly two pairs exist where you meant two.

Why the whole query should not be one component

If the entire assembled query title=red & blue&page=2 is encoded as a single component, the structural equals signs and ampersands are encoded as well, and the output is title%3Dred%20%26%20blue%26page%3D2. That text is a valid single value, but it is no longer a query string with two pairs. A server will see one field with a strange name, and the page parameter the application was waiting for will be missing, so the application may show a default page or return an error with no hint that encoding caused it. The last case in the checks list shows this exact output. Encoding a whole query as one component is correct only when the query itself is being placed inside another value, for example as a redirect target, where it must be treated as data by the outer URL. In that case the layers are intentional.

Other characters that need care

Besides the ampersand and the equals sign, several characters change the meaning of a value if left raw. The percent sign needs %25, because a raw percent starts an escape. The hash sign needs %23, the question mark is allowed but often encoded as %3F for safety, and the slash is encoded as %2F when it is data and not a path separator. The plus sign needs %2B if the receiver applies the form rule. A quotation mark and a less-than sign are unsafe in links and should be encoded as %22 and %3C. Component encoders cover all of these in one step, which is why using one is more reliable than choosing characters by hand.

Non-ASCII characters in a value

Characters outside ASCII are encoded as the percent-escaped bytes of their UTF-8 form. A title containing an accented letter becomes a pair of escapes for the two bytes of the letter, so the accented e, U+00E9, is %C3%A9. A three-byte character such as the euro sign produces three escapes, %E2%82%AC. Decoding reverses the process and requires the bytes to form valid UTF-8. A title with a Japanese word of two characters produces six escapes, three for each character. Long encoded strings are therefore several times longer than the text they carry, which matters for systems that limit the length of a URL, and many servers and browsers set limits of a few thousand characters. This is why a component decoder is strict about complete sequences. If the encoded text is cut in the middle of a character, the decoder cannot rebuild the original and reports the input as invalid.

Repeated names and arrays

Query strings have no built-in type system. A list of values is usually sent as repeated names, as in tag=red&tag=blue, or with a bracket or comma convention that depends on the framework. A value that itself contains a comma, such as red,blue, can be mistaken for two items if the receiver splits on commas, so the delimiter used for lists needs to be encoded as data when it appears inside an item, or a different list convention is required. The encoder in this guide handles only the characters of one value and leaves these conventions to the application that defines them.

Scope of the tool

The encoder and decoder work on one component of text at a time. They do not assemble parameter names, decide how repeated parameters behave, parse a complete URL, or check that a URL is reachable. They apply UTF-8 percent escapes to the characters that need them and reverse the process strictly. Use them to prepare a single value for insertion and to inspect a single value taken from a link. For a full URL with several parts, treat each part with its own rules, and keep the structure characters outside the encoded values.

Reproduce the checks

An ampersand in a title

Open the Url Encoder Decoder and enter:

Calculation mode
encode
Text or encoded input
red & blue

Expected output:

Text

red%20%26%20blue

The ampersand belongs to the field value.

An equals sign in data

Open the Url Encoder Decoder and enter:

Calculation mode
encode
Text or encoded input
a=b

Expected output:

Text

a%3Db

The data character is escaped.

Restore the title

Open the Url Encoder Decoder and enter:

Calculation mode
decode
Text or encoded input
red%20%26%20blue

Expected output:

Text

red & blue

The round trip preserves the original characters.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks