URL Structure & Paths (Absolute vs Relative)
Master URL anatomy and path types. Learn when to use absolute vs. relative paths, understand URL components, and avoid common linking mistakes.
What is a URL?
A URL (Uniform Resource Locator) is the address used to locate resources on the web. Understanding URL structure is essential for creating correct links.
Pro-Tip
Anatomy of a URL
Let's break down a complete URL into its components:
https://www.example.com:443/blog/article?id=123&sort=date#section-2
Scheme Subdomain Domain Port Path File Query String Fragment
Full breakdown:
Protocol/Scheme: https://
Host/Domain: www.example.com
Subdomain: www
Domain: example
TLD: .com
Port: :443 (optional, default for https)
Path: /blog/article
Query String: ?id=123&sort=date
Fragment: #section-2URL Components Explained:
1. Scheme/Protocol
Defines how to access the resource
http://- Unencrypted HyperText Transfer Protocolhttps://- Secure HTTP (encrypted with SSL/TLS)ftp://- File Transfer Protocolmailto:- Email addresstel:- Telephone numberfile://- Local file system
2. Domain/Host
The server address where the resource is located
www.example.com
www (subdomain)
example (domain name)
.com (top-level domain)3. Port
Optional - network port on the server
- Default ports (usually omitted):
- HTTP: port 80
- HTTPS: port 443
- Custom ports must be specified:
:8080,:3000
4. Path
Location of the resource on the server
/blog/articles/html-guide
/blog (directory)
/articles (subdirectory)
/html-guide (file or route)5. Query String
Parameters passed to the resource
?id=123&category=html&sort=date
? (starts query string)
id=123 (parameter 1)
& (separator)
category=html (parameter 2)
& (separator)
sort=date (parameter 3)6. Fragment/Hash
Specific section within the resource
#section-2
Jumps to element with id="section-2"Absolute URLs
Absolute URLs contain the complete address, including protocol and domain.
<!-- Complete URL with all parts -->
<a href="https://www.example.com/blog/article.html">Article</a>
<!-- External site link -->
<a href="https://www.wikipedia.org">Wikipedia</a>
<!-- Secure vs non-secure -->
<a href="https://secure-site.com">HTTPS Site</a>
<a href="http://old-site.com">HTTP Site</a>
<!-- With port number -->
<a href="http://localhost:3000/app">Local Dev Server</a>When to Use Absolute URLs:
- Links to external websites
- Email links (
hrefin HTML emails) - Canonical URLs (SEO)
- Open Graph tags (social media)
- RSS/XML feeds
- API endpoints
Advantages:
- Work from anywhere (portable)
- Clear and unambiguous
- Required for external resources
Disadvantages:
- Longer and more verbose
- Harder to move site to new domain
- Can't easily test on different environments
Relative URLs
Relative URLs specify a path relative to the current page's location.
<!-- Relative to current directory -->
<a href="about.html">About Page</a>
<!-- Subdirectory -->
<a href="blog/article.html">Blog Article</a>
<!-- Parent directory -->
<a href="../index.html">Parent Level</a>
<!-- Root-relative (starts with /) -->
<a href="/contact">Contact Page</a>
<!-- Same page anchor -->
<a href="#section-2">Jump to Section 2</a>
<!-- Query string only -->
<a href="?page=2">Page 2</a>Types of Relative URLs:
1. Document-Relative
Relative to the current page's location
Current page: /blog/articles/post.html
<a href="other-post.html">
/blog/articles/other-post.html
<a href="images/photo.jpg">
/blog/articles/images/photo.jpg2. Root-Relative
Relative to the site root (starts with /)
Current page: /blog/articles/post.html
<a href="/about">
/about (from root)
<a href="/images/logo.png">
/images/logo.png (from root)3. Protocol-Relative
Uses current protocol (http or https)
<!-- Inherits protocol from current page -->
<a href="//example.com/page">Link</a>
If on https://mysite.com:
https://example.com/page
If on http://mysite.com:
http://example.com/pageWhen to Use Relative URLs:
- Internal site navigation
- Images, CSS, and JavaScript files on same domain
- Development (works on localhost and production)
- Easier site migration
Advantages:
- Shorter and cleaner
- Portable between environments (dev, staging, prod)
- Works with or without www prefix
- Automatically uses correct protocol
Disadvantages:
- Can be confusing with multiple directory levels
- Break if page moves to different location
- Don't work in emails or external contexts
Navigating Directory Structures
Understanding how to navigate directories with relative paths:
Example Site Structure:
website-root/
index.html
about.html
blog/
index.html
post-1.html
post-2.html
images/
logo.png
banner.jpg
assets/
css/
style.css
js/
script.jsSpecial Path Symbols:
| Symbol | Meaning | Example |
|---|---|---|
/ | Root directory (at start) | /index.html |
./ | Current directory | ./about.html |
../ | Parent directory (up one level) | ../index.html |
../../ | Up two levels | ../../index.html |
Query Strings & Parameters
Query strings pass data to pages through the URL:
<!-- Single parameter -->
<a href="search.html?q=html">Search for HTML</a>
/search.html?q=html
<!-- Multiple parameters -->
<a href="shop.html?category=books&sort=price&page=2">Books</a>
/shop.html?category=books&sort=price&page=2
<!-- With fragment -->
<a href="article.html?id=123#section-2">Article Section</a>
/article.html?id=123#section-2
<!-- URL encoding special characters -->
<a href="search.html?q=html%20%26%20css">Search "html & css"</a>
/search.html?q=html%20%26%20cssCommon URL Encoding:
| Character | Encoded | Usage |
|---|---|---|
| Space | %20 or + | hello%20world |
| & | %26 | tom%26jerry |
| = | %3D | 2%2B2%3D4 |
| ? | %3F | what%3F |
| / | %2F | http%3A%2F%2F |
Common URL Mistakes
Mistake #1: Missing Protocol
<a href="www.example.com">Link</a>
<!-- Treated as relative path: /www.example.com --><!-- Correct -->
<a href="https://www.example.com">Link</a>Mistake #2: Wrong Relative Path
<!-- From /blog/post.html, trying to link to /about.html -->
<a href="about.html">About</a>
<!-- Goes to /blog/about.html (wrong!) --><!-- Correct options -->
<a href="../about.html">About</a> <!-- Up one level -->
<a href="/about.html">About</a> <!-- Root-relative -->Mistake #3: Spaces in URLs
<a href="my page.html">Link</a>
<!-- Breaks! Spaces must be encoded --><!-- Correct -->
<a href="my-page.html">Link</a> <!-- Use hyphens -->
<a href="my%20page.html">Link</a> <!-- Or encode -->Mistake #4: Mixed Protocols
<!-- On HTTPS site, linking to HTTP -->
<img src="http://example.com/image.jpg">
<!-- Mixed content warning! --><!-- Correct -->
<img src="https://example.com/image.jpg"> <!-- HTTPS -->
<img src="//example.com/image.jpg"> <!-- Protocol-relative -->Best Practices
Do
- Use root-relative paths for internal navigation
- Use absolute URLs for external links
- Encode special characters in URLs
- Use lowercase for consistency
- Use hyphens instead of spaces
- Test all links after moving files
- Use trailing slashes consistently
Don't
- Forget protocol on external links
- Use spaces in URLs
- Mix relative path levels unnecessarily
- Use absolute URLs for internal links
- Include sensitive data in query strings
- Create overly long URLs
- Use special characters without encoding
Quick Reference
| Path Type | Example | Starts With | Use Case |
|---|---|---|---|
| Absolute | https://example.com/page | http:// or https:// | External links |
| Root-relative | /about | / | Internal navigation |
| Document-relative | page.html | Filename | Same directory |
| Parent | ../page.html | ../ | Up one level |
| Current | ./page.html | ./ | Current directory |
| Protocol-relative | //example.com/page | // | Inherit protocol |