iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

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

CharacterEncoded as
space%20 (or + in query strings)
!%21
"%22
#%23
&%26
+%2B
/%2F
?%3F
@%40
é%C3%A9 (UTF-8 bytes for é)

Encoding in code

LanguageFunction
JavaScriptencodeURIComponent("hello world")hello%20world
PHPurlencode("hello world")
Pythonurllib.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

Test yourself

Q1. A space in a URL is encoded as…
Q2. In JS, encode a URL component with…
Q3. A literal & inside a query value must be encoded as…

Discussion

Loading…