Search results for “html url encoding” mix three different things. Each one makes text safe for a different place, and using the wrong one leaves the text either broken or double-escaped. Here is how to tell them apart.
Side by side
The same text, encoded three ways. URL encoding uses encodeURIComponent, HTML encoding uses this site's HTML encoder, and Base64 encodes the UTF-8 bytes. All values were produced by running the code.
| Text | URL encoding | HTML encoding | Base64 |
|---|---|---|---|
| Tom & Jerry | Tom%20%26%20Jerry | Tom & Jerry | VG9tICYgSmVycnk= |
| <b> | %3Cb%3E | <b> | PGI+ |
| "quoted" | %22quoted%22 | "quoted" | InF1b3RlZCI= |
| 50% off | 50%25%20off | 50% off | NTAlIG9mZg== |
| café | caf%C3%A9 | café | Y2Fmw6k= |
What each one is for
- URL encoding (percent-encoding) protects data inside a URL. Characters with a job in URL syntax, such as
&,=and?, and characters a URL cannot hold, such as spaces, become%XX. Decoded by URL parsers and servers. URL Encoder. - HTML encoding (character references) protects text inside an HTML document.
<,>and&become<,>and&so they are shown as text instead of being read as markup. Decoded by the HTML parser. HTML Encoder. - Base64 turns arbitrary bytes into 64 safe characters so binary data can travel through text-only channels. Output grows by about a third. Not a form of encryption. Base64 tool.
Links in HTML need two layers
A link to a search for Tom & Jerry written into a web page goes through both encodings, in this order:
- Percent-encode the value:
Tom%20%26%20Jerry - Build the URL:
/search?q=Tom%20%26%20Jerry&page=2 - HTML-escape it for the attribute:
/search?q=Tom%20%26%20Jerry&page=2
The browser reverses the steps: the HTML parser turns & back into &, and the server later decodes %26. Skipping step 1 cuts the value at the ampersand. Skipping step 3 is usually tolerated by browsers, but it is invalid HTML and can be misread.
Common mix-ups
&inside a decoded URL. The URL was HTML-escaped twice, or copied from HTML source. Decode it with the HTML decoder, not the URL decoder.%20showing up on a page. A URL-encoded value was displayed as text without being decoded first.- Base64 in a URL losing characters. Standard Base64 uses
+and/, which have meaning in URLs. Use Base64URL or percent-encode the value; see %2B.