🔥 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)
SEO••By Komal Nakrani

Noindex Pages in Astro: Meta Robots and X-Robots-Tag

Noindex pages in Astro: a noindex prop on your layout, frontmatter for content pages, X-Robots-Tag headers for files and previews, and the robots.txt trap.

#Astro#SEO#noindex#Robots

To noindex a page in Astro, render <meta name="robots" content="noindex"> in its <head>, usually through a noindex prop on your base layout. For files that have no <head>, such as PDFs, or for a whole preview deployment, send an X-Robots-Tag: noindex HTTP header instead. Do not also block the page in robots.txt, because crawlers that cannot fetch the page never see the noindex.

Key Takeaways

  • A noindex prop on the base layout covers .astro pages; a frontmatter field covers Markdown and MDX.
  • noindex, follow keeps links on the page crawlable.
  • Use the X-Robots-Tag header for PDFs, feeds and staging sites.
  • Remove noindexed pages from your sitemap so you do not send mixed signals.

A noindex prop on the layout

---
// src/layouts/Base.astro
interface Props {
  title: string;
  noindex?: boolean;
}
const { title, noindex = false } = Astro.props;
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>{title}</title>
    {noindex && <meta name="robots" content="noindex, follow" />}
  </head>
  <body>
    <slot />
  </body>
</html>

Use it on pages that should not appear in search, such as thank-you pages, internal search results and tag pages with one post:

---
// src/pages/thank-you.astro
import Base from '../layouts/Base.astro';
---
<Base title="Thanks for your message" noindex>
  <h1>Thanks, we will reply within one working day.</h1>
</Base>

Noindex from Markdown frontmatter

For content collections, add an optional field to the schema and pass it through:

// src/content.config.ts (schema excerpt)
schema: z.object({
  title: z.string(),
  noindex: z.boolean().default(false),
}),
---
// src/pages/blog/[id].astro (excerpt)
const { post } = Astro.props;
---
<Base title={post.data.title} noindex={post.data.noindex}>

Writers can then set noindex: true in a post’s frontmatter without touching code. Our content collections guide covers schemas in depth.

X-Robots-Tag for files and previews

A meta tag only works in HTML. For a PDF in public/, or to keep an entire preview deployment out of search, use a header. On Cloudflare Pages and Netlify, add rules to public/_headers:

/downloads/*
  X-Robots-Tag: noindex

To noindex a whole preview site, have your build write that header only when the build is not production, or use your host’s setting for preview deployments. On server-rendered routes, middleware can set it per response:

// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';

export const onRequest = defineMiddleware(async (context, next) => {
  const response = await next();
  if (context.url.pathname.startsWith('/account/')) {
    response.headers.set('X-Robots-Tag', 'noindex');
  }
  return response;
});

The _headers file tip explains the file’s syntax and limits.

The robots.txt trap

Disallow in robots.txt stops crawling, not indexing. A blocked URL can still be indexed from links, shown with no description, and Google never sees its noindex tag. If you want a page out of search results, allow crawling and use noindex. Use Disallow for crawl budget, such as endless filter URLs. The robots.txt tip shows how to generate the file.

Keep the sitemap consistent

A sitemap tells search engines a page is worth indexing, so leave noindexed pages out of it. See excluding pages from the sitemap for filter patterns. If you start from a theme in our catalog, add the prop to the base layout once and every page can use it; the pages docs show where layouts live.

FAQ

How do I check that a page is noindexed?

View the page source and search for name="robots", or check the response headers for X-Robots-Tag. In Google Search Console, URL Inspection reports “Excluded by noindex tag” once Google has recrawled it.

Should I use noindex, nofollow or noindex, follow?

Usually noindex, follow. It keeps the page out of results while letting crawlers follow its links. nofollow is rarely needed on your own pages.

How long does it take for a noindexed page to disappear from Google?

It drops out after Google recrawls the page, which can take days or weeks. For urgent cases, use the Removals tool in Search Console as a temporary measure while the noindex takes effect.

Can I noindex pages from astro.config.mjs?

There is no built-in config option. Use the layout prop, a frontmatter field, a _headers rule or middleware, depending on whether the page is HTML, a file or server-rendered.