The Anatomy of a URL: scheme, host, path, query and fragment

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.

Frequently Asked Questions

Which part of a URL goes to the server, and which does not?

The fragment (everything after #) never reaches the server—it's purely a browser-side anchor used to scroll to an element. The path and query are visible to the server (and land in server logs and site analytics). Understand the boundary: the server sees from after the scheme through the query, minus the fragment, which exists only in the browser's address bar. That's also why 'put a token in the fragment to avoid leaking it' is common but unreliable—it never hits the server, so the backend can't authenticate with it.

Why do Chinese characters or spaces in a URL fail sometimes but work other times?

Standard URLs only allow ASCII alphanumerics and a few symbols; Chinese, spaces and friends must be percent-encoded. Browsers usually auto-encode them in the address bar, so 'it won't open' mostly happens when: ①the backend concatenates raw Chinese into the URL without encoding; ②logs/links keep an unencoded space that truncates the link; ③the browser doesn't auto-encode in certain contexts (cookie, hand-assigned location). Correct practice: use encodeURIComponent for parameter *values* and encodeURI for the whole URL—never hand-join strings. Verify the %xx mapping with a URL encoder/decoder tool.

What is the relationship between URL, URI and URN?

In one line, URI ⊇ URL, URN. URI (Uniform Resource Identifier) is the widest concept—anything that *identifies* a resource, whether reachable or not. URL (Locator) is one subtype: it identifies *and* gives access method and location (scheme + host + path), like https://a.com/x. URN (Name) is another subtype: a stable, location-independent name, like urn:isbn:.... In practice 'URL' and 'URI' are used interchangeably; almost any endpoint/documentation saying URI accepts a URL. URI is merely the broader superset.

Why does the same address parse to different port/path across browsers and sites?

Because many parts are optional with defaults: ①a missing port takes the scheme default (http→80, https→443); ②prefixes like 'http://' or 'www.' may be filled in; ③a relative path resolves against the current page into an absolute one; ④whether an empty query parameter is kept, and whether the fragment participates in key-sorting, varies across implementations. So 'visually identical' hrefs can split into different ports/paths under different parsers. Don't eyeball it—feed the same URL into a URL parser/formatter and compare fields; most seeming mysticism is just default-value rules.

← Back to Blog