Relative URLs and absolute URLs
In this lesson
- The parts of a URL
- Absolute URLs
- Relative and root-relative URLs
- Using ../ to go up a folder
Short answer
An absolute URL is the full address, with protocol and domain, like https://example.com/about/. A relative URL is a short path from the current page, like about.html. Use absolute URLs for other websites. Use relative or root-relative URLs for pages on your own site, so links keep working if you move or test the site.
Parts of a URL
https://www.example.com/products/pili-tarts.html?size=box#reviews
| Part | Example | Name |
|---|---|---|
| https:// | https | Protocol or scheme |
| www.example.com | Domain name | |
| /products/pili-tarts.html | Path | |
| ?size=box | Query string | |
| #reviews | Fragment |
Absolute URLs
<a href="https://www.phivolcs.dost.gov.ph/">PHIVOLCS volcano updates</a>
Absolute URLs always work, no matter which page they are on. Use them for external links, canonical tags, and Open Graph URLs.
Relative URLs
Imagine this folder:
my-site/
index.html
about.html
products/
index.html
pili-tarts.html
images/
logo.png
| From | To | Relative URL |
|---|---|---|
| index.html | about.html | about.html |
| index.html | products/pili-tarts.html | products/pili-tarts.html |
| products/pili-tarts.html | index.html (home) | ../index.html |
| products/pili-tarts.html | images/logo.png | ../images/logo.png |
../ means go up one folder. ./ means this folder.
Root-relative URLs
<a href="/products/pili-tarts.html">Pili tarts</a>
A path that starts with / begins at the root of your domain. It works from any page on the site, so many developers prefer it for navigation menus. One catch: when you open files directly from your computer, / points to your hard drive root. Use Live Server or a real host to test root-relative links.
Try it: write the paths
You are on products/pili-tarts.html. Fill in each href so it points to the right file using the folder map above.
Live preview
Quick quiz
Common mistakes
- Using relative paths for external sites. example.com without https:// is treated as a local file.
- Hard-coding localhost URLs. Links to localhost break once the site is online.
- Wrong case in file names. Most web servers treat Logo.png and logo.png as different files.
Your action
Tick each item when you can do it. Your progress saves on this device.
Key terms
- URL
- Uniform Resource Locator, a web address.
- Absolute URL
- A full URL with protocol and domain.
- Relative URL
- A path relative to the current page.
- Root-relative URL
- A path starting with / from the domain root.
- Protocol
- The scheme like https:// that starts a URL.
- Query string
- Parameters after ? in a URL.
Questions beginners ask
Which is better for SEO?
Both work for internal links. Search engines resolve relative URLs. Use absolute URLs for canonical tags.
Why do my links work offline but break online?
Check letter case and folder names. Servers are often case sensitive.
What is a trailing slash?
The / at the end, like /about/. It usually points to a folder's index.html.
Related lessons
Need a website built or fixed while you practice? The team at Remar Digital can help.