“Invalid URL”, “URI malformed” and links that break at a space all come from the same rule: a URL can carry only a small set of ASCII characters. Anything else has to be percent-encoded, written as % followed by two hex digits for each byte. This guide lists what is allowed, what is not, and what each common tool does when it meets an invalid character.
Characters that are allowed
- Unreserved:
A–Z a–z 0–9 - . _ ~. Always allowed, never need encoding. - Reserved delimiters:
: / ? # [ ] @and! $ & ' ( ) * + , ; =. Allowed where they do their job (separating the parts of the URL). When they are part of a value, encode them. - Percent sign: only as the start of a
%XXsequence. A literal % is written %25.
Characters that are never allowed unencoded
RFC 3986 does not allow these anywhere in a URL. The last column shows what the WHATWG URL parser, which browsers and Node.js use, does with each one in a path. It was generated by running the parser on https://example.com/a?b with each character in place of the question mark.
| Character | Name | Encode as | Browser parser, in a path |
|---|---|---|---|
| SP | Space | %20 | %20 |
| " | Quotation mark | %22 | %22 |
| < | Less-than sign | %3C | %3C |
| > | Greater-than sign | %3E | %3E |
| \ | Reverse solidus (backslash) | %5C | turned into / |
| ^ | Circumflex accent | %5E | %5E |
| ` | Grave accent (backtick) | %60 | %60 |
| { | Left curly bracket | %7B | %7B |
| | | Vertical line (pipe) | %7C | left as-is |
| } | Right curly bracket | %7D | %7D |
Two things stand out. The parser repairs most invalid characters by encoding them, which is why a link with a space often still works in a browser. But it leaves | and ^ untouched in a path, so a URL containing them is passed on unchanged even though RFC 3986 does not allow them. Encode them yourself. It also turns a backslash into a forward slash, which silently changes the path. Non-ASCII characters such as é are converted to UTF-8 and percent-encoded: /caf%C3%A9.
The errors you will see
TypeError: Invalid URLfromnew URL()when the text cannot be parsed as an absolute URL. Real results from Node.js:new URL("example.com/page")→TypeError: Invalid URL(no scheme)new URL("https://exa mple.com/")→TypeError: Invalid URL(space in the host). Browsers are not fully consistent here: in our tests Chrome accepted this host by encoding the space as %20, while Node.js rejected it, as the WHATWG URL Standard requires.new URL("https://example.com:99999/")→TypeError: Invalid URL(port out of range)
URIError: URI malformedfromdecodeURIComponentwhen a%is not followed by two hex digits, or when the bytes are not valid UTF-8. The URL Decoder shows the exact position and can decode leniently. Every cause and fix is in how to fix URI malformed.- 400 Bad Request from a server that rejects a raw space or other invalid character in the request line instead of repairing it.
A colon in the first segment
A relative URL cannot start with a segment that contains a colon. RFC 3986 forbids it because a:b would be read as a URL with the scheme a. The parser does exactly that: resolved against https://example.com/dir/, a:b becomes a:b, while ./a:b becomes https://example.com/dir/a:b. Prefix the path with ./ or encode the colon as %3A.
How to fix an invalid URL
- Make sure it has a scheme, such as
https://, if it is meant to be absolute. - Encode each value separately with a component encoder (
encodeURIComponent, Python'squote(s, safe='')) before joining the URL together. Do not encode the finished URL as one value. - Never encode an already encoded value: that produces %2520.
- Check the result in the URL Parser. If it parses, the parser will also list any parameter that still looks encoded.