Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

When should a space in a URL be percent-encoded?

Quick answer

Use %20 when a space needs to appear in a URL path or in a query string written as a URL. Use + when the query data is being serialized as application/x-www-form-urlencoded, where + represents a space. The right choice depends on the URL context and serialization convention. Encode the value, not the whole URL indiscriminately, and check how the receiving system interprets it. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a URL with a space in a page name. Should I replace the space with %20 or +?

Geez

For a space in a URL path, use %20. Percent-encoding represents a character with % followed by hexadecimal digits. A space can be represented as %20 or + in some contexts, but the appropriate form depends on how the data is being used. In particular, + represents a space in application/x-www-form-urlencoded data, while %20 is used for spaces in URLs. [S1]

For example, if the original path is /team notes/, its space-encoded form is /team%20notes/. Don’t treat + as a universal substitute for a space in a path.

Follow-up question

What about a search term in the query string? Is %20 still right there?

Geez

If you are writing a URL query string and want the space represented explicitly in the URL, %20 is a suitable choice. For example, assuming the query value is red shoes:

Before: /search?q=red shoes After: /search?q=red%20shoes

There is a distinction if the query data is serialized as application/x-www-form-urlencoded: in that format, a space is represented as +. So a form-encoded value could be red+shoes. These conventions depend on context, so don’t swap the characters without considering how the receiving system reads the value. [S1]

Follow-up question

Can I just encode every space in the complete URL, or replace every space with a plus sign?

Geez

Avoid blanket replacements. Identify the component containing the space—the path or a query value—and use the convention for that context. A broad replacement can change characters outside the value you intended to edit. And using + when the data is not being serialized as form data may not express the intended space. The distinction is that %20 is used for spaces in URLs, while + represents a space in application/x-www-form-urlencoded data. [S1]

A practical editing pattern is to leave the URL structure intact and encode only the value that contains the space. If you are unsure how a particular system expects the value, check its documentation.

Follow-up question

Can you give me a small reference I can reuse while editing links?

Geez

Use this compact reference; the examples are placeholders:

  1. Path space: /folder name/page → /folder%20name/page
  2. Query space in a URL: /search?q=red shoes → /search?q=red%20shoes
  3. Form-encoded query value: red shoes → red+shoes

Before saving a link, ask: “Is this a path, a URL query, or form-encoded data?” If you cannot tell which convention the system expects, check its documentation. The appropriate representation depends on the URL context and serialization convention; the examples above do not establish how every receiving system will interpret a hand-edited URL. [S1]

A practical checklist

  • Locate the space: identify whether it is in a path or query value.

  • Use %20 for a space written in a URL path or URL query.

  • Use + for a space when the data uses application/x-www-form-urlencoded serialization.

  • Encode the relevant value rather than applying a blind replacement across the complete URL.

  • Check the receiving system’s convention if its interpretation is unclear.

A few more useful answers

Is `+` always the same as `%20` in a URL?

No. + represents a space in application/x-www-form-urlencoded data, while %20 is used for spaces in URLs. Use the form that matches the context. [S1]

What should I do if I do not know how a site expects its query string?

Check the site or tool’s documentation for its serialization convention. The appropriate form depends on the context, and the convention for a particular system may need to be confirmed.

Sources

References used for this guide.

  1. [S1] Percent-encoding — MDN Glossary
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work