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.

πŸ“‚ Web & Internet ⏱ About 9 minutes πŸ“Ά Beginner πŸ› οΈ Pairs with: URL Encoder

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
In this guide
  1. Anatomy of a URL
  2. Scheme, host and port
  3. Path, query string and fragment
  4. Percent-encoding: why %20 means a space
  5. Relative vs absolute URLs
  6. Common mistakes
  7. Try it: URL Encoder
  8. 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
PartWhat it does
schemeHow to connect: https, http, ftp, mailto. https means HTTP over TLS encryption.
userinfoOptional user:password@ credentials. Rare on the web today β€” and a classic phishing trick, because https://google.com@evil.com really goes to evil.com.
hostThe domain name (resolved by DNS) or an IP address. Case-insensitive.
portOptional. Defaults are 443 for HTTPS and 80 for HTTP β€” browsers hide them when they are the default.
pathWhich resource on the server. Case-sensitive on most servers: /Page and /page can be different pages.
queryKey–value data after ?, joined by &. Search terms, filters, tracking codes live here.
fragmentAfter #: 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.

CharacterEncodedWhy
space%20Not allowed in a URL at all
&%26Would split query parameters
=%3DWould split a key from its value
?%3FWould start a new query string
#%23Would start a fragment
%%25The escape character itself
Γ© / δΈ­%C3%A9 / %E4%B8%ADUTF-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.

The + 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 %20 into %2520 (the % itself gets encoded). The server then decodes once and still sees %20 as 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.com goes to evil.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.

Open the URL Encoder β†’

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.