HTML URL Encode
URLs can only contain a restricted set of ASCII characters. Anything else — spaces, accented letters, symbols — must be URL-encoded as a percent sign followed by the hex code of the byte.
Common encodings
| Character | Encoded as |
|---|---|
| space | %20 (or + in query strings) |
! | %21 |
" | %22 |
# | %23 |
& | %26 |
+ | %2B |
/ | %2F |
? | %3F |
@ | %40 |
é | %C3%A9 (UTF-8 bytes for é) |
Encoding in code
| Language | Function |
|---|---|
| JavaScript | encodeURIComponent("hello world") → hello%20world |
| PHP | urlencode("hello world") |
| Python | urllib.parse.quote("hello world") |
Tip: Encode query-string values, not the whole URL. Encoding the
? or = would break the structure.Example
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML URL Encode</title>
</head>
<body>
<h1>HTML URL Encode</h1>
<p>This is a demo page for the "HTML URL Encode" lesson.</p>
</body>
</html>
Try it Yourself »
Exercise
A space in a URL must be encoded as which percent code?
https://site.com/search?q=hello
world
Percent sign plus two hex digits.
Discussion
Loading…