How URLs Work: Structure, Encoding, Query Strings & Fragments
Every link you click is a small instruction manual. Learn to read each part of a URL β scheme, host, port, path, query, fragment β and how percent-encoding keeps special characters safe.
What you will learn
- Read any URL part by part and say what each piece does
- Know which characters must be percent-encoded β and why
- Tell a query string from a fragment, and know which one the server sees
- Spot the difference between encodeURI and encodeURIComponent
- Avoid the double-encoding bug that breaks links and APIs
- Anatomy of a URL
- Scheme, host and port
- Path, query string and fragment
- Percent-encoding: why %20 means a space
- Relative vs absolute URLs
- Common mistakes
- Try it: URL Encoder
- Frequently asked questions
Anatomy of a URL
A URL is an address with a fixed grammar. Take this one apart:
https://user:pass@example.com:8080/path/to/page?query=1&x=2#section βββ¬βββ ββββββ¬βββββ ββββββ¬βββββ ββ¬β βββββββ¬ββββββ ββββββ¬βββββ ββββ¬ββββ scheme userinfo host port path query fragment
| Part | What it does |
|---|---|
scheme | How to connect: https, http, ftp, mailto. https means HTTP over TLS encryption. |
userinfo | Optional user:password@ credentials. Rare on the web today β and a classic phishing trick, because https://google.com@evil.com really goes to evil.com. |
host | The domain name (resolved by DNS) or an IP address. Case-insensitive. |
port | Optional. Defaults are 443 for HTTPS and 80 for HTTP β browsers hide them when they are the default. |
path | Which resource on the server. Case-sensitive on most servers: /Page and /page can be different pages. |
query | Keyβvalue data after ?, joined by &. Search terms, filters, tracking codes live here. |
fragment | After #: a position inside the page. Handled by the browser only β it is never sent to the server. |
Scheme, host and port
The scheme decides the rules. https:// encrypts the connection; http:// does not, and anything you send can be read on the network. Modern browsers warn about plain HTTP on login and payment pages for exactly this reason.
The host is a domain name, and DNS turns it into an IP address before any connection happens β that step is covered in our DNS guide. Ports are the finer address: a server can run many services on one IP, one per port. Web traffic uses 80/443 by convention, which is why you almost never see a port in a link.
Path, query string and fragment
The path is the part servers route on: /guides/what-is-dns means βthe guides section, the DNS pageβ. A trailing slash matters more than it looks β /guides and /guides/ can be different resources, and servers often redirect one to the other.
The query string is data, not an address: ?q=shoes&sort=price asks the server for shoes sorted by price. Order of parameters usually does not matter, but duplicate keys (?tag=a&tag=b) do occur, and servers handle them differently β some take the first, some the last, some treat them as a list.
The fragment (#section) never leaves your browser. It scrolls to an anchor or drives a single-page app's view. Two consequences: fragments don't affect what the server returns, and they don't appear in server logs β which is why βshare linksβ that carry state in the fragment can be both private and confusing.
Percent-encoding: why %20 means a space
URLs may only contain a limited set of characters. Everything else β spaces, non-English letters, &, =, ? inside a value β must be percent-encoded: replaced by % plus two hexadecimal digits of its byte value. A space is byte 0x20, so it becomes %20.
| Character | Encoded | Why |
|---|---|---|
| space | %20 | Not allowed in a URL at all |
& | %26 | Would split query parameters |
= | %3D | Would split a key from its value |
? | %3F | Would start a new query string |
# | %23 | Would start a fragment |
% | %25 | The escape character itself |
| Γ© / δΈ | %C3%A9 / %E4%B8%AD | UTF-8 bytes, each percent-encoded |
In JavaScript there are two encoders and they are not interchangeable: encodeURI() encodes a whole URL but leaves : / ? & = # alone (they are structural); encodeURIComponent() encodes a single value and escapes those too. Encoding a whole URL with encodeURIComponent destroys it; encoding a value with encodeURI leaves a & inside it that splits your parameter in two.
+ trap. In query strings, + traditionally means a space (from HTML forms), but in a path + is a literal plus sign. If your code decodes + as a space everywhere, a filename like C++ becomes C . Decode + as a space only in query strings.Relative vs absolute URLs
An absolute URL carries everything (https://veryusefultools.com/guides/). A relative URL borrows the missing parts from the current page: what-is-dns.html (same folder), ../index.html (one folder up), /guides/ (from the site root). Relative links keep a site portable β move the whole site to a new domain and every internal link still works. Absolute links are what you share with other people.
Common mistakes
- Double encoding. Encoding an already-encoded value turns
%20into%2520(the%itself gets encoded). The server then decodes once and still sees%20as literal text. Encode exactly once, as late as possible. - Encoding the whole URL with encodeURIComponent. The
://and/get escaped and the link dies. - Putting secrets in the query string. URLs are logged by servers, proxies and analytics, and shared in screenshots. Tokens and passwords belong in headers or POST bodies, never in a link.
- Assuming the path is case-insensitive. The host is; the path usually isn't.
- Trusting the text before the @.
https://paypal.com@evil.comgoes toevil.com. Read the host β the part after the last@and before the next/.
Try it: URL Encoder
Paste a messy URL into the URL Encoder and watch each character get encoded β then decode it back. It is the fastest way to see percent-encoding working on your own links.
Keep learning
Frequently asked questions
What is the difference between a URL, a URI and a URN?
A URI is the general concept: any identifier for a resource. A URL is a URI that also tells you how to reach it (a location). A URN is a URI that names something without saying where it is, like urn:isbn:9780134685991. Every URL is a URI, but not every URI is a URL.
How long can a URL be?
The standard sets no limit, but browsers do β roughly 2,000 characters in older Internet Explorer, around 8,000+ in modern Chrome. Long URLs also break in emails and chats. If your query string is growing past a few hundred characters, send the data as a POST body instead.
Can a URL contain Chinese, Arabic or emoji?
Yes β browsers display them, but underneath they are percent-encoded UTF-8 bytes (and domain names use punycode, e.g. xn--fiqs8s for δΈε½). Copy a link with non-English characters and you will see the encoded form in the clipboard.
Why do some links show %20 and others show + for spaces?
Both mean βspaceβ in a query string, from different traditions: %20 is percent-encoding, + comes from HTML form encoding. In the path, only %20 is correct β a + there is a literal plus.
Does the fragment (#β¦) get sent to the server?
No. The browser strips it before making the request. That is why single-page apps can store view state in the fragment without the server ever seeing it β and why a shared link with a fragment sometimes doesn't reproduce what the sender saw.