๐Ÿ”ฅ All-Access Pass โ€” $199 USD (Regular $499) ยท Unlock all 2,836 Astro themes โž”
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deploymentโ€ขโ€ขBy Komal Nakrani

Astro Base Path: Deploy to a Subdirectory

Use the Astro base path option to deploy under a subdirectory like /docs/: set base and site, prefix links with BASE_URL, and fix assets from public/ that 404.

#Astro#Deployment#Base Path#Configuration

To deploy Astro to a subdirectory, set base in astro.config.mjs to the folder path, for example base: '/docs', and set site to the domain. Astro then prefixes its own bundled assets and routes with that path, but links you write by hand and files from public/ need the prefix too, which you get from import.meta.env.BASE_URL.

Key Takeaways

  • base is the path your site lives under; site is the domain. Most subdirectory deploys need both.
  • Astro rewrites URLs for its own output (/_astro/ assets, getStaticPaths routes), not for strings you type.
  • Build every internal link and public/ file path from import.meta.env.BASE_URL.
  • Test with astro build and astro preview, which serve the site under the base path.

Set base and site

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  site: 'https://www.example.com',
  base: '/docs',
});

The site is now served at https://www.example.com/docs/. In dev, Astro serves it at http://localhost:4321/docs/ as well, so broken links show up before you deploy. Common cases are GitHub Pages project sites (base: '/repo-name'), docs under a marketing domain, and sites behind a reverse proxy path.

import.meta.env.BASE_URL holds the base. Whether it ends in a slash depends on your trailingSlash setting, so join paths with a small helper rather than string concatenation:

// src/utils/url.ts
export function url(path = '') {
  const base = import.meta.env.BASE_URL.replace(/\/$/, '');
  const clean = path.replace(/^\//, '');
  return clean ? `${base}/${clean}` : `${base}/`;
}
---
// src/components/Nav.astro
import { url } from '../utils/url';
---
<nav>
  <a href={url()}>Home</a>
  <a href={url('guides/')}>Guides</a>
  <a href={url('changelog/')}>Changelog</a>
</nav>

With base: '/docs', these render as /docs/, /docs/guides/ and /docs/changelog/. Markdown links written as /guides/ are not rewritten, so either use relative links in content or post-process them with a remark plugin.

Fix files from public/

Anything in public/ is copied as-is, and a path like /favicon.svg points at the domain root, not your base. Use the helper there too:

---
// src/layouts/BaseLayout.astro
import { url } from '../utils/url';
---
<head>
  <link rel="icon" type="image/svg+xml" href={url('favicon.svg')} />
  <link rel="sitemap" href={url('sitemap-index.xml')} />
</head>

Images imported from src/ through astro:assets are handled for you, because Astro writes them to /_astro/ and already knows the base. That is one more reason to import images rather than reference them from public/.

Canonicals and the sitemap

Astro.site is the domain only; Astro.url.pathname already includes the base. So a canonical built as new URL(Astro.url.pathname, Astro.site) gives the right absolute URL. @astrojs/sitemap also respects base. See our canonical URLs tip and the site option tip.

Test before you deploy

# Terminal
npm run build
npm run preview

Open http://localhost:4321/docs/, click through the navigation and watch the network panel for 404s. A missing prefix shows up as a request to the domain root. Our GitHub Pages guide walks through a full project-site deploy with base, and the astro.config docs cover the other options. Every theme in our Astro theme catalog works under a base path once links go through a helper like this.

FAQ

What is the base option in Astro?

base is the URL path your site is served under, such as /docs or /my-repo. Astro uses it to prefix bundled assets and routes, and exposes it as import.meta.env.BASE_URL.

Astro does not rewrite links you type as strings. Prefix internal links and public/ file paths with import.meta.env.BASE_URL, or use relative links.

Do I need both site and base?

For a subdirectory deploy, usually yes. site is the domain for absolute URLs such as canonicals and sitemaps; base is the path. Set site without the base path.

Does base affect astro dev?

Yes. The dev server and astro preview both serve the site under the base path, so you can catch missing prefixes locally.