Skip to main content
Back to Blog

URL Anatomy: Parsing and Building Robust Links

Scheme, host, path, query, and fragment explained—plus how to parse and build URLs safely in code and APIs.

Kashyap Thakar
4 min
WebURLHTTP
URL Anatomy: Parsing and Building Robust Links

The Uniform Resource Locator (URL) is the address system of the web. While we type them daily, their internal structure is more complex than it appears. Understanding this anatomy is crucial for frontend routing, API design, and SEO.

The Components

Let's dissect a typical URL:
https://api.example.com:8080/v1/users?sort=desc#profile

  • Protocol (Scheme): https - Defines how to communicate (HTTP, FTP, Mailto).
  • Subdomain: api - A subset of the main domain.
  • Domain: example.com - The unique address of the server.
  • Port: 8080 - The specific gate on the server (default is 80 for HTTP, 443 for HTTPS).
  • Path: /v1/users - The specific resource or page being requested.
  • Query String: ?sort=desc - Key-value pairs for filtering or tracking.
  • Fragment (Hash): #profile - A specific section within the resource (client-side only).

The Danger of String Concatenation

Junior developers often build URLs like this:

const url = "https://api.com/search?q=" + query;

If query contains spaces, &, or ?, this breaks the URL structure. Always use a proper builder or encoding functions.

URL Components Deep Dive

Protocol (Scheme)

The protocol determines how the browser communicates with the server:

  • http:// - Hypertext Transfer Protocol (unencrypted)
  • https:// - HTTP Secure (encrypted with TLS/SSL)
  • ftp:// - File Transfer Protocol
  • mailto: - Opens email client
  • file:// - Local file system

Domain Hierarchy

Domains are hierarchical, read from right to left:

api.v2.example.com
└─ com (Top-Level Domain)
   └─ example (Second-Level Domain)
      └─ v2 (Subdomain)
         └─ api (Subdomain)

Query Parameters

Query strings use key=value pairs separated by &:

?category=electronics&sort=price&page=2
└─ category = "electronics"
└─ sort = "price"
└─ page = "2"

Values must be URL-encoded if they contain special characters. For example, "hello world" becomes "hello%20world".

Fragment (Hash)

The fragment (everything after #) is never sent to the server. It's used for:

  • Linking to specific sections of a page (anchor links)
  • Client-side routing in Single Page Applications (SPAs)
  • JavaScript state management

URL Encoding Requirements

Certain characters have special meanings in URLs and must be encoded:

CharacterEncodedReason
Space%20 or +Not allowed in URLs
&%26Parameter separator
#%23Fragment identifier
=%3DKey-value separator

Relative vs. Absolute URLs

URLs can be absolute (complete) or relative (partial):

  • Absolute: https://example.com/path/to/page - Contains everything needed
  • Relative: /path/to/page - Resolved relative to current domain
  • Protocol-relative: //example.com/path - Uses current protocol (http or https)

Tools for the Job

Debugging complex URLs with multiple query parameters can be a headache. Here's how our tools help:

  • URL Parser: Visualize every component of a long link. Perfect for understanding complex URLs, debugging routing issues, and learning URL structure.
  • URL Builder: Safely construct links with many parameters without worrying about encoding errors. Great for API testing and creating shareable links.
  • URL Validator: Check format and accessibility. Verify URLs before using them in production or sharing with users.
  • URL Encoder: Encode special characters in URLs and query parameters to ensure they work correctly.
  • URL Decoder: Decode encoded URLs to see their original content, useful for debugging and understanding encoded data.

Part of the ThenCatch blog. Learn more about us or browse more guides.