The site option in astro.config.mjs is your site’s final production URL, such as https://www.example.com. Astro uses it to build absolute URLs: it powers Astro.site, canonical links, @astrojs/sitemap, RSS feeds and Open Graph image URLs. Without it, those features either fail or quietly produce relative URLs that search engines and social networks cannot use.
Key Takeaways
- Set
siteto the full origin, includinghttps://, with no path unless you deploy under one.Astro.siteis aURLobject whensiteis set andundefinedwhen it is not.- The sitemap integration and
@astrojs/rssneedsiteto produce valid output.siteis the domain;baseis the subdirectory. They are separate options.
Set it once
// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
site: 'https://www.example.com',
integrations: [sitemap()],
});
Use the exact origin visitors and search engines should see: choose www or the bare domain, and match whatever your host redirects to. A mismatch means every canonical tag points at a URL that redirects.
What uses it
| Feature | What happens without site |
|---|---|
Astro.site |
undefined, so code like new URL('/x/', Astro.site) throws |
@astrojs/sitemap |
Logs a warning and does not write a sitemap |
@astrojs/rss |
Needs a site value to build item links |
Canonical and og:url tags |
Must be absolute; you have nothing to build them from |
og:image |
Social networks ignore relative image URLs |
Build absolute URLs from it in your layout:
---
// src/components/Head.astro
import ogDefault from '../assets/og-default.png';
const { title, image = ogDefault } = Astro.props;
const canonical = new URL(Astro.url.pathname, Astro.site);
const ogImage = new URL(image.src, Astro.site);
---
<link rel="canonical" href={canonical} />
<meta property="og:url" content={canonical} />
<meta property="og:title" content={title} />
<meta property="og:image" content={ogImage} />
Astro.url.pathname already includes any base, so this works for subdirectory deploys too. Our canonical URLs tip and sitemap tip cover both features in detail.
site versus base
site is where the site lives on the internet. base is the path below that origin. For a GitHub Pages project site:
// astro.config.mjs
export default defineConfig({
site: 'https://username.github.io',
base: '/my-repo',
});
Do not write site: 'https://username.github.io/my-repo' and also set base; put the path in base only. Our base path tip explains what base changes.
Vary it per environment
Preview deployments have their own URLs, but you normally want canonicals on previews to keep pointing at production. If you do need the preview URL, for example to test Open Graph tags, read it from an environment variable at config time:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: process.env.SITE_URL ?? 'https://www.example.com',
});
Set SITE_URL in the host’s preview environment only. astro.config.mjs runs in Node.js, so process.env is available there; inside components, use import.meta.env.SITE, which Astro sets from this option. See our environment variables guide and preview deployments tip.
Check it in the build
# Terminal
npm run build
grep -o 'rel="canonical" href="[^"]*"' dist/index.html
The output should show your production origin. If it is missing or relative, site is not set or a component is ignoring it. All themes in our Astro theme catalog read the domain from a single config value, and the astro.config docs list the rest of the top-level options.
FAQ
What should the site option be in Astro?
Your full production origin, such as https://www.example.com, including the protocol. Add no trailing path unless you deploy under one, and in that case put the path in base.
Why is Astro.site undefined?
Astro.site is only set when site is defined in astro.config.mjs. Add it and restart the dev server.
Does the site option change where Astro serves pages locally?
No. site only affects generated absolute URLs. The dev server still runs on localhost. The base option is the one that changes local paths.
Can I use import.meta.env. SITE in components?
Yes. Astro exposes the configured site as import.meta.env.SITE. Astro.site gives you the same value as a URL object.