The answer first: seven segments, and only one never reaches the server
https://user:pw@example.com:8080/path/to/page?q=hello%20world&lang=zh#section-2
Split it and you get seven parts: scheme, userinfo, host, port, path, query, fragment. Only the fragment is never sent to the server, while the query does land in access logs. Hold that boundary and every encoding rule below becomes one question: can this character appear literally in this segment?
The anatomy: what each segment does
scheme userinfo host port path query fragment
┌──────┐┌───────┐┌─────────┐┌────┐┌───────────┐┌──────────────┐┌────────┐
https:// user:pw @ example.com :8080 /path/to/page ?q=hello&lang=zh #section-2
└──┴──┘ └──────────────┘
authority
| Segment | Example | Role |
|---|---|---|
| scheme | https |
protocol; sets the default port and encryption |
| userinfo | user:pw |
rarely used; plaintext credentials, largely deprecated by browsers |
| host | example.com |
target hostname or IP |
| port | 8080 |
port; defaults to the scheme's when omitted |
| path | /path/to/page |
resource path, /-delimited |
| query | q=hello&lang=zh |
key-value pairs, &-separated, values encoded |
| fragment | #section-2 |
browser-side anchor, never sent |
What the server sees, and what lands in logs
server sees: path + query + host + port + scheme
server never sees: fragment (after #) — a purely local browser anchor
Two practical consequences:
- The query enters access logs and analytics, so putting tokens or ID numbers there is the same as writing them into a log;
- The fragment never reaches the server and therefore cannot authenticate anything; hiding a token after
#only keeps it out of logs, while frontend JavaScript still reads it.
Encoding rules differ by segment
A URL only allows ASCII alphanumerics plus safe symbols such as . / - _ ~; everything else must be encoded:
space → %20
Chinese → each byte as UTF-8, then %xx; e.g. 『中』 = %E4%B8%AD
& → %26 (must be encoded inside a value, or it's read as a separator)
Hand-joining strings trips over some Chinese character or symbol almost every time. Use encodeURIComponent for values and encodeURI for the whole URL.
Where %20, + and & belong
The same + reads as a space in a query — a legacy form-encoding convention — but as a literal plus in a path. An & inside a query value is data and must be written %26, or it splits the value into another parameter. A # used as data anywhere must be %23, or everything after it becomes the fragment.
Default ports are not "no port"
When the port is omitted, the parser fills in the scheme default: 80 for http, 443 for https. So https://a.com and https://a.com:443 point at the same resource while remaining different strings; normalise before signing or building cache keys. Relative paths behave the same way, resolving against the current page into an absolute one.
Verify each segment with a parser
Do not hunt for delimiters by eye. Feed this URL to the URL parser and have it list scheme/host/port/path/query/fragment, then compare against a deliberately broken link — the segment you assembled wrong stands out immediately:
https://a.com:8443/api/v1/items?tag=dev%20tools&lang=en#top
30-second verification
from urllib.parse import urlsplit
u = urlsplit('https://oltool.net:443/reference/json.html?lang=zh#faq')
for k in ('scheme', 'netloc', 'hostname', 'port', 'path', 'query', 'fragment'):
print(f"{k} = {getattr(u, k)!r}")
scheme = 'https'
netloc = 'oltool.net:443'
hostname = 'oltool.net' # ← port stripped
port = 443
path = '/reference/json.html'
query = 'lang=zh'
fragment = 'faq' # ← never sent to the server
The fact worth keeping is the difference between hostname and netloc: the first excludes the port, the second includes it.