CHAPTER 17 · HTML

URLs and file paths: find the right file from the right place

A page can link to another file in its folder, inside a smaller folder or one level above. Learn to read these paths, understand a full web address and repair a link by checking where the browser starts looking.

Download example files (ZIP)

A path is a set of directions

Suppose a friend is standing outside Room 6. “Go next door” is useful only because you know the starting place. A relative path works the same way: it gives directions from the current document's location.

A full address supplies more of the location itself. It is like giving the building's complete address rather than directions from a nearby room.

Read a full web address

Consider this fictional address:

Example fragment — use inside a complete program
https://example.com/club/plan.html?day=friday#bring
PartExampleMeaning
SchemehttpsHow the resource is addressed; here it uses HTTPS
Hostexample.comThe named server or website host
Path/club/plan.htmlThe resource location on that host
Query?day=fridayExtra information the site may use
Fragment#bringA part of the document, often an element id

A query does not automatically filter a page. The site needs to use it. A fragment normally identifies a location within the returned document; it is not sent as part of the HTTP request to the server. Other schemes exist, including mailto and tel from the previous chapter.

Read a small folder tree

Use the downloadable examples or make this structure yourself. A folder is a container for files and other folders. Here, pages is inside the main practice folder.

Example fragment — use inside a complete program
html-practice/
  path-home.html
  pages/
    path-session.html

From path-home.html, the session file is inside pages. From path-session.html, the home file is one level above. These are two different starting points, so their directions differ.

Useful path forms

Href valueWhere it looks
notice.htmlIn the current document's folder
./notice.htmlAlso in the current folder
pages/notice.htmlIn a child folder named pages
../notice.htmlIn the parent folder, one level above
../../notice.htmlTwo folder levels above
/notice.htmlAt the root of the current website when served over HTTP or HTTPS
https://example.com/notice.htmlAt a full external address

Use forward slashes in web paths. A Windows path such as D:\College\notice.html describes one computer's file system; it is not a portable website address. Do not publish such paths for your visitors.

A leading slash has a special meaning. When you directly open a file with file://, /notice.html does not mean the root of your practice project. Use the relative examples here for local practice. Test website-root paths on a local web server or a deployed site later.

Resolve a relative file path
  1. 1Locate the current document
  2. 2Read its containing folder
  3. 3Follow child folders or ../
  4. 4Check the exact destination filename

Moving the document changes the starting point for ordinary relative paths.

Complete program: path-home.html

Save this file at the top of the practice folder.

path-home.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Folder Practice Home</title>
</head>
<body>
  <h1>Start the folder practice</h1>
  <p>The session page is inside the pages folder.</p>
  <p><a href="pages/path-session.html">Open the session in its folder</a></p>
</body>
</html>
Browser previewDocument title: Folder Practice Home

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a heading, a location explanation and a link. The browser must find a pages folder beside this file and then find path-session.html inside that folder.

Complete program: pages/path-session.html

Create pages, then save the next file inside it as path-session.html. The displayed filename includes the folder to show its correct location.

pages/path-session.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Session Inside a Folder</title>
</head>
<body>
  <h1>You reached the session folder</h1>
  <p>This file is one folder below the home page.</p>
  <p><a href="../path-home.html">Return to the folder practice home</a></p>
</body>
</html>
Browser previewDocument title: Session Inside a Folder

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

The two dots mean move up one directory level. After that move, the browser looks for path-home.html. They do not mean “go to the previous browser page”; that is a different idea.

Names, spaces and URL encoding

Prefer short lowercase filenames with hyphens, such as practice-notes.html. They are easier to type and compare. Avoid changing the letter case between the file and its references.

Some characters need a special spelling in a URL. Percent encoding represents a byte with % and two hexadecimal digits. A space in a path can be written as %20. For example, a file named practice notes.txt can be linked with files/practice%20notes.txt. Do not rename the actual file to include the characters %20 unless that is really the name you intend.

An unescaped # begins a fragment and ? begins a query. If either is part of a filename, it needs appropriate encoding. Do not encode the whole address blindly: structural slashes and the scheme colon have jobs of their own. A plus sign does not universally mean a space in a URL path.

In HTML attribute text, an ampersand separating query parameters should be written as &amp;. HTML character references and URL percent encoding solve different problems. The HTML parser reads the attribute first; the resulting address is then interpreted as a URL.

HTML fragment — part of a document
<a href="https://example.com/search?topic=html&amp;level=beginner">Search the example catalogue</a>

This is an address-reading example; example.com does not provide a working catalogue search for this lesson.

When the starting location changes

If you move a page into another folder, recheck its relative links and image paths. Their starting location has changed. A base element can deliberately change how relative URLs are resolved, but it affects many links at once. We will study it with document information later; do not add one just to hide an unexplained broken path.

Problem and solution: it works on the home page only

Problem: you copy href="pages/path-session.html" into a file already inside pages. The browser looks for pages/pages/path-session.html.

Solution: start from the current file's folder. For a sibling file in pages, use its filename. For a file above pages, use ../ followed by the filename. Draw the folder tree before guessing.

Common mistakes and useful tips

  • Starting every path from the project folder instead of the current document.
  • Using a backslash or a private drive letter in a public URL.
  • Confusing ../ with the browser Back action.
  • Expecting a query to create a search feature by itself.
  • Writing a filename's spaces, case or extension differently from the actual file.
  • Assuming a leading slash works the same way for a local file and a served website.
  • Moving files without retesting incoming and outgoing links.

Practice questions

  1. What is the starting point for an ordinary relative file path?
  2. From path-home.html, how do you reach pages/path-session.html?
  3. From that session file, how do you return to path-home.html?
  4. What is the difference between a query and a fragment?
  5. Which URL spelling represents a space in a path?
  6. Build the shown folder tree, test navigation in both directions and then deliberately rename one file. Explain and repair the broken link.
Check your practice answers
  1. The current document's location, unless a base URL deliberately changes it.
  2. Use href="pages/path-session.html".
  3. Use href="../path-home.html".
  4. A query supplies address parameters a site may use. A fragment identifies a part of a document and is not sent in the HTTP request.
  5. %20. Prefer uncomplicated hyphenated names when you control the filenames.
  6. Save both files in their correct folders. The renamed file requires matching changes in links that point to it. Check both directions again after the repair.

Interview questions

How does a relative URL differ from an absolute URL?

A relative URL needs a base location to resolve its destination. An absolute URL includes its scheme and enough address information to identify the destination without that document-relative step.

What does a root-relative path mean on an HTTPS website?

A path beginning with one slash starts at the current website's root rather than the current page's folder. That is different from a full address to a different host.

Are HTML entities and URL encoding interchangeable?

No. HTML character references describe characters in HTML source. Percent encoding describes bytes within URL components. Use the form required at each stage rather than replacing one with the other.

Why do links often break after moving a file?

Relative addresses are resolved from a location. Moving the document changes that location, so the same relative text may now point somewhere else.

Quick revision

  • Draw the folder tree and start from the current document.
  • Use forward slashes, exact names and ../ for parent folders.
  • Distinguish full addresses, website-root paths and document-relative paths.
  • Queries, fragments and percent encoding have different roles.