Static Site Links Break Under a Subdirectory? Check Base Paths and Asset URLs

Diagnose static site links broken when deployed in a subdirectory. Test base paths, asset URLs, nested route refreshes, and server fallbacks.

Published
Reading time
8 min read
Static Site Links Break Under a Subdirectory? Check Base Paths and Asset URLs
On this page

A site that works at / can link to the wrong pages or lose its styles when deployed under /docs/. The usual cause is a mismatch between the URL paths in the generated files and the path where the server publishes them. This guide shows how to identify that mismatch, reproduce it locally, and distinguish it from missing files and server-routing problems.

Key Takeaways

  • A URL beginning with / starts at the domain root, not at the site’s deployment subdirectory.
  • A path-relative URL resolves against the current page URL, so it can behave differently on a nested page.
  • Check the requested URL and response in browser developer tools before changing build settings: a wrong path, a missing file, and a missing route fallback need different fixes.
  • Test generated HTML and deployed pages, including a direct refresh of a nested route.

How URLs resolve at the root and under /docs/

Assume a page has no HTML <base> element, which can change the base used for relative URLs. The following examples use https://example.com/ as a fictional origin.

URL in HTMLFrom https://example.com/From https://example.com/docs/
Root-relative: /guide/https://example.com/guide/https://example.com/guide/
Path-relative: guide/https://example.com/guide/https://example.com/docs/guide/
Absolute: https://example.com/guide/https://example.com/guide/https://example.com/guide/
Root-relative: /assets/site.csshttps://example.com/assets/site.csshttps://example.com/assets/site.css
Path-relative: assets/site.csshttps://example.com/assets/site.csshttps://example.com/docs/assets/site.css

Root-relative and absolute URLs can still load under /docs/, but they may point to a different site at the domain root. A successful response is therefore not proof that the URL is correct.

Path-relative URLs need attention on nested pages too. From /docs/guide/, assets/site.css resolves to /docs/guide/assets/site.css, while ../assets/site.css resolves to /docs/assets/site.css. A trailing slash matters: a browser resolves relative paths from /docs/guide differently from /docs/guide/.

Advertisement

Reproduce the problem locally

This framework-free lab creates the same small site at both / and /docs/. Each copy identifies itself as ROOT or DOCS, so you can see when a link or image reaches the wrong copy even if it loads successfully.

You need Python 3 and a browser. In a new empty directory, save this as make_site.py:

from pathlib import Path

output = Path("site")

for prefix, label in (("", "ROOT"), ("docs", "DOCS")):
    base = output / prefix
    (base / "assets").mkdir(parents=True, exist_ok=True)
    (base / "guide").mkdir(parents=True, exist_ok=True)

    (base / "assets" / "site.css").write_text(
        f"/* {label} stylesheet */\nbody {{ font-family: sans-serif; }}\n",
        encoding="utf-8",
    )
    (base / "assets" / "logo.svg").write_text(
        f'<svg xmlns="http://www.w3.org/2000/svg" width="120" height="30">'
        f'<text x="5" y="20">{label}</text></svg>',
        encoding="utf-8",
    )
    (base / "index.html").write_text(
        f'''<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>{label} home</title>
  <link rel="stylesheet" href="/assets/site.css">
  <link rel="stylesheet" href="assets/site.css">
</head>
<body>
  <h1>{label} home</h1>
  <ul>
    <li><a href="/guide/">Root-relative guide</a></li>
    <li><a href="guide/">Path-relative guide</a></li>
    <li><a href="http://localhost:8000/guide/">Absolute guide</a></li>
  </ul>
  <p>Root-relative image: <img src="/assets/logo.svg" alt="Site label"></p>
  <p>Path-relative image: <img src="assets/logo.svg" alt="Site label"></p>
</body>
</html>''',
        encoding="utf-8",
    )
    (base / "guide" / "index.html").write_text(
        f'''<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>{label} guide</title>
  <link rel="stylesheet" href="../assets/site.css">
</head>
<body>
  <h1>{label} guide</h1>
  <p><a href="../">Back to this site's home</a></p>
  <p><a href="guide/">Nested path-relative example</a></p>
  <img src="../assets/logo.svg" alt="Site label">
</body>
</html>''',
        encoding="utf-8",
    )

Generate the files and start the local server:

python3 make_site.py
cd site
python3 -m http.server 8000

Open http://localhost:8000/ and http://localhost:8000/docs/. Keep the server running while you use the matrix below. Stop it with Ctrl+C when finished. This example has not been executed for this article; the paths in the matrix describe the expected behavior.

Test matrix

Open the browser’s Network panel, reload each page, and inspect the request URLs as well as the visible labels. The two stylesheets have the same visual rule, so use their request URLs or response comments to tell them apart.

CheckAt /At /docs/
Root-relative guide link/guide/ → ROOT guide/guide/ → ROOT guide, not DOCS
Path-relative guide link/guide/ → ROOT guide/docs/guide/ → DOCS guide
Absolute guide link/guide/ → ROOT guide/guide/ → ROOT guide, not DOCS
Root-relative image/assets/logo.svg → ROOT/assets/logo.svg → ROOT, not DOCS
Path-relative image/assets/logo.svg → ROOT/docs/assets/logo.svg → DOCS
Root-relative stylesheet/assets/site.css/assets/site.css, not the DOCS copy
Path-relative stylesheet/assets/site.css/docs/assets/site.css
Open and refresh nested guide/guide/ serves its index.html/docs/guide/ serves its index.html

On either guide page, the deliberately incorrect guide/ link resolves to a further nested path: /guide/guide/ or /docs/guide/guide/. The ../ home link and ../assets/ references instead point back to that copy’s parent directory.

The local server in this example serves real directory indexes; it does not demonstrate a single-page application fallback. Also, the lab’s absolute link is intentionally tied to localhost:8000 and is not a deployment pattern.

Advertisement

Diagnose the deployed site

  1. Find the intended URL. For example, if the deployed guide belongs under /docs/, decide whether its canonical page is /docs/guide/ or another path supported by your host.
  2. Inspect the request. In the Network panel, check the full URL, status, response content, and redirects for the failed link, image, or stylesheet. Check the page’s Elements panel to see the actual href or src emitted by the build.
  3. Compare with the deployed files. If the browser requests /assets/site.css but the file is deployed at /docs/assets/site.css, the URL is wrong. If it requests /docs/assets/site.css and that file is absent from the deployed output, investigate the build or upload instead.
  4. Test the nested URL directly. Paste it into a new tab and refresh it. For a multipage static site, confirm the corresponding generated file and the host’s directory-index behavior. For a client-side route without a corresponding file, check whether the host is configured to serve the appropriate application entry point for that route.
  5. Inspect the response, not just the status. A server fallback may return an HTML page for a missing CSS or image request. That can produce a successful status while the asset still fails to load. Scope any fallback so it does not silently hide missing assets.

A redirect can also affect relative URL resolution by changing the final page URL. Check the final address, especially whether it ends in a slash.

Fix the build and deployment together

Choose a consistent public base path for the site. For a site intended exclusively for /docs/, generated navigation and local asset references should resolve under /docs/, unless a resource intentionally lives elsewhere. For a build intended to be movable, path-relative references can work, but they must be correct from every generated page—not just the home page.

Static-site generators and frameworks differ in how they configure a base path, generate asset URLs, and handle client-side routes. No current generator documentation or configuration example was verified for this article. Before publication, consult the current official documentation for your specific generator and version, apply its documented setting, and rebuild. Do not assume that changing a server mount alone rewrites URLs already present in generated HTML.

If assets intentionally use a separate absolute URL, such as a CDN, check that the deployed pages reference the intended host and that the assets are actually available there. Treat that separately from the site’s navigation base path.

Pre-release checks

  1. Build the site using the intended deployment base path. Inspect the generated HTML before upload, including links, images, stylesheets, scripts, metadata, srcset values, and any HTML <base> element. Also check CSS url(...) references and URLs generated by JavaScript.

  2. As a quick first pass, replace public with your actual output directory and search for double-quoted attributes beginning with /:

    grep -RIn '="/' public || true
    

    Review each match; some root-relative or protocol-relative URLs may be intentional. This is only a heuristic: it misses single-quoted, unquoted, dynamically generated, and many CSS URLs.

  3. Deploy to a staging path matching the intended production prefix. Visit the home page, follow navigation, inspect images and stylesheets in the Network panel, and open and refresh representative nested routes directly.

  4. For each failure, record the requested URL, final URL after redirects, status, and response type. Compare them with the generated output and host routing rules before deciding whether to change the base path, restore an asset, or configure a route fallback.

  5. Repeat the checks after the final build and deployment; a local preview at / alone does not exercise the /docs/ prefix.

Common Mistakes

  • Assuming /assets/ means “this site’s assets.” It means assets at the origin root, regardless of the current page’s subdirectory.
  • Using one relative path at every page depth. assets/site.css from a nested route is not the same URL as assets/site.css from the home page.
  • Treating every 404 as a base-path problem. The request may be correct while the file is missing, or the host may lack a route fallback.
  • Accepting a 200 response without inspecting it. A fallback page can be returned instead of the requested asset.
  • Testing only through navigation. Direct entry and refresh exercise server routing as well as browser-side navigation.

Conclusion

Start with the browser’s resolved request URL, then compare it with the generated files and the server’s routing behavior. That separates incorrect base paths from missing assets and refresh failures—and gives you a concrete way to verify the fix under the real deployment prefix.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.