Free auditStart free
GuideAstro · Crawling

Astro robots.txt

Serve robots.txt from Astro’s public directory or generate it from the configured site URL, then verify the deployed rules.

3 min
read
4
sources
Oct 24, 2026
updated
Scaplet Editorial Team
author
In one line

For a static Astro site, put a plain-text robots.txt in public/; Astro copies it to the root of the built site. If the sitemap URL depends on site in astro.config.mjs, generate /robots.txt with a src/pages/robots.txt.ts endpoint and return text/plain. Check the production response: robots rules control crawling, not access or guaranteed deindexing.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Which Astro setup fits?

Choose static or generated output

Setup

Static rules

Use
public/robots.txt
Check
Confirm the host publishes the file at /robots.txt
Setup

Sitemap URL comes from config

Use
A src/pages/robots.txt.ts endpoint
Check
Set the site URL and return plain text
Setup

Rules vary by request

Use
Server-rendered endpoint or deployment layer
Check
Confirm your adapter supports the runtime behavior

Astro's project-structure guide says files in public/ are copied into the build folder untouched, and its own example tree lists public/robots.txt. A generated endpoint is useful when you want to derive the sitemap URL from Astro’s configured site value and avoid manually keeping two URLs in sync.

Keep the file small and specific. A broad Disallow: / can block crawling across the whole site, while a rule for /preview/ only makes sense if that path exists and should not be fetched. Robots rules are public instructions for cooperative crawlers; they do not protect private routes, and another crawler may ignore them.

public/robots.txt
User-agent: *
Allow: /

Sitemap: https://example.com/sitemap-index.xml
02Check it

Generate the sitemap line from Astro config

Astro’s sitemap integration can create a sitemap index for a site with multiple sitemap files. The configured site value must be your canonical production origin. This endpoint example mirrors the integration’s documented pattern; use the final public URL your build actually emits.
src/pages/robots.txt.ts
import type { APIRoute } from 'astro'

export const GET: APIRoute = ({ site }) => {
  if (!site) throw new Error('Set site in astro.config.mjs')

  const sitemap = new URL('sitemap-index.xml', site)
  const body = `User-agent: *\nAllow: /\n\nSitemap: ${sitemap.href}\n`

  return new Response(body, {
    headers: { 'Content-Type': 'text/plain; charset=utf-8' },
  })
}
03How it works

Verify the deployed file

  1. 1

    Build the production output

    Inspect the generated robots.txt or start the production adapter. A local development route is not proof of the deployed response.

  2. 2

    Request the canonical host

    Open https://example.com/robots.txt; check the status, plain-text content type, host, and sitemap URL.

  3. 3

    Test important paths

    Make sure important pages are not disallowed. Keep rules limited to paths you intend crawlers to skip.

  4. 4

    Check sitemap consistency

    Open every sitemap URL listed and confirm it contains canonical, indexable URLs that return successfully.

When a deployment uses a path prefix or a non-root domain, verify that the robots file still lives at the crawler-facing origin root. If a CDN serves a cached copy, purge or wait for that cache after changing the rules, then request the public URL again.

For the related setup, see Astro XML sitemaps, robots.txt rules, and noindex directives.

Sources · 4

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • astro.build
  • google.com
  1. 01Project structure: public directory | Astro Docsdocs.astro.build · checked 2026-10-24
  2. 02@astrojs/sitemap | Astro Docsdocs.astro.build · checked 2026-10-24
  3. 03Introduction to robots.txt | Google Search Centraldevelopers.google.com · checked 2026-10-24
  4. 04Block search indexing with noindex | Google Search Centraldevelopers.google.com · checked 2026-10-24
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Where should robots.txt go in an Astro project?

For a static file, put it in the root public/ directory; Astro copies it to the deployed site root. Use a src/pages/robots.txt.ts endpoint when the content must be generated.

02How do I add a sitemap to Astro robots.txt?

Add a Sitemap: line with the absolute URL of the sitemap or sitemap index your deployment serves. Astro’s sitemap integration documents a sitemap index; verify the exact output path for your configuration.

03Can robots.txt add noindex to an Astro page?

No. Robots rules control crawling. To keep a crawlable public page out of Google, serve a page-level noindex directive and allow Google to fetch the page.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane