Skip to content
← SyntaxBench reference notes

Safely URL Encode Special Characters in URLs

Contents
AI-generated illustration; not taken on location · Concept diagram showing characters transformed into percent-encoded sequences for URLs
AI-generated illustration; not taken on location · Conceptual illustration of URL percent-encoding rulesAI-generated illustration

URLs are designed to work across many systems, but they can only safely carry a limited set of characters. Characters outside that set, or characters with special structural meaning in a URL, generally need percent-encoding: a percent sign followed by two hexadecimal digits representing the character’s UTF-8 byte value. Encoding decisions depend on where the text appears in the URL and whether you are encoding a single component or a complete address. This article explains which characters must be encoded, how common encoding modes behave, how form-urlencoded values treat spaces and plus signs differently, what tools and limits apply, and how to avoid problems such as double encoding.

Which characters must be encoded in URLs

A URL may safely contain only ASCII alphanumeric symbols, certain unreserved characters, and reserved control characters used for URL structure. Characters outside this allowed set generally require percent-encoding.

Unreserved characters include hyphen, underscore, period, and tilde. These may appear in text strings without encoding because they do not have special meaning in URL syntax.

Characters that often require encoding include space, quotation mark, less-than sign, greater-than sign, hash, percent, and pipe. Their common encoded forms are:

  • Space: %20
  • Double quote: %22
  • Greater-than sign: %3E
  • Hash: %23
  • Percent sign: %25
  • Pipe: %7C

Non-ASCII text is encoded using its UTF-8 byte sequence. For example, 上海+中國 becomes %E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B. The question mark and ampersand are structural delimiters in many contexts, so whether they are encoded depends on the encoding mode and the part of the URL being encoded.

Because encoding rules change by location, you should identify the target component before choosing an encoding method. A value intended for a query parameter often needs different treatment from a value appearing in a path segment.

Component encoding versus complete URL encoding

URL component mode encodes an individual path segment, query key, or query value. It treats structural delimiters such as &, =, /, ?, and # as characters that must be encoded when they appear as literal text. For example, café & tea becomes caf%C3%A9%20%26%20tea.

Complete URL mode preserves reserved URL delimiters because it is intended for encoding an entire address while leaving its structure intact. Encoded delimiters such as %2F remain encoded when the result is decoded. Complete URL mode is not a URL validator, so a successfully encoded string is not necessarily a valid or reachable URL.

The practical choice depends on the task. When encoding a user-supplied query value, use component mode for that value rather than encoding the whole URL at once. Encoding the complete URL can leave literal & or = characters inside a value unencoded, which may change how a server parses the query string. Encoding an individual component protects the value while preserving the surrounding URL structure where appropriate.

If you are building a link from separate parts, encode each user-supplied part as a component and then assemble the parts with the literal delimiters your application needs.

Form-urlencoded value rules

Form value mode follows application/x-www-form-urlencoded rules, which differ from general percent-encoding in two common cases. A space is encoded as + rather than %20, and a literal plus sign is encoded as %2B.

The string ? and the Mysterions can therefore appear as %3F+and+the+Mysterians or %3F%20and%20the%20Mysterians, depending on the mode. In form value mode, the plus form is expected for spaces.

Decoding in this mode is strict UTF-8. Malformed percent escapes or unpaired surrogates produce an error and leave the source untouched. This means a form-encoded value containing an incomplete escape such as %2 should not be silently repaired; it should be treated as invalid input.

Use form value mode when preparing data for HTML form submission or similar form-urlencoded endpoints. Use general component mode when the value will be placed in a URL query string outside a form submission context, and confirm which convention the receiving service expects.

Decide which mode matches your endpoint

Start by checking whether the destination expects form-urlencoded data. An HTML form with enctype="application/x-www-form-urlencoded" normally sends spaces as plus signs, so + is correct there. If you are building a query parameter by hand for an API call, %20 is usually safer because it preserves the space character exactly as a percent-encoded byte.

Handle literal plus signs carefully

Because + means space in form-urlencoded data, a real plus sign must be sent as %2B. If you forget this step, a decoder may turn your literal plus into a space and change the value.

Treat malformed escapes as invalid

An incomplete sequence such as %2 or a lone % at the end of a value should not be guessed at or completed automatically. Strict decoding reports an error and leaves the source unchanged, so your application should reject or flag the value rather than attempt recovery.

Compare the two space conventions side by side

Situation Form value encoding General component encoding
Space + %20
Literal plus %2B %2B
Question mark %3F %3F

Use this table as a quick reference, but verify the receiving service’s documented behavior before sending data.

Reader questions

  • Does your target endpoint document spaces as + or %20?
  • Does the value contain literal plus signs that must survive decoding?
  • What should your application do when decoding fails on a malformed escape?

Tool behavior and practical limits

The SyntaxBench URL codec provides observable behaviors that help explain how encoding and decoding tools work in practice. Component mode encodes individual path segments, query keys, and query values, including structural delimiters. Complete URL mode uses encodeURI/decodeURI semantics and preserves URL delimiters. Form value mode applies form-urlencoded rules and reports strict decoding errors without modifying the source.

The supplied packet does not state a practical character limit for URLs in the SyntaxBench URL codec. Readers should not assume a specific maximum length. If you need to know whether a particular tool enforces a URL length limit, check the tool’s documentation or test it with representative input before relying on it in production.

Tool behavior also varies for non-ASCII characters. Some implementations normalize or encode UTF-8 bytes differently, and older systems may not support the full Unicode range. When Chinese, accented text, or other non-ASCII characters must appear in a URL, verify the encoded output with the receiving service rather than assuming all tools produce identical results.

Best practices for valid and secure links

Treat user input as literal text, not as URL structure. Encode each user-supplied component separately before assembling the final URL. Do not insert raw input directly into a path segment, query value, or fragment, because characters such as &, =, ?, #, and % can change the meaning of the URL or introduce injection risks.

Choose the encoding mode for the destination context. Use component mode for query values and path segments. Use form value mode for HTML form submissions. Use complete URL mode only when you intentionally need to preserve an entire URL’s delimiters.

Avoid double encoding. If a value is already percent-encoded, encoding it again turns % into %25, so %20 becomes %2520. Decode first only when you are certain the input is already encoded; otherwise encode raw text once.

Use platform libraries where available instead of writing manual replacement logic. Libraries handle edge cases such as UTF-8 conversion, reserved characters, and strict decoding errors more reliably than ad hoc code.

After encoding, verify that the URL parses correctly in the target environment. Remember that encoding produces a syntactically protected string, not a guarantee that the destination exists or is safe to visit. Confirm link behavior with the receiving service and apply any additional validation required by your application.