deindexpages.com
Platform how-tos

Deindexing pages in Next.js and static sites

With frameworks and static generators, nothing is toggled in a dashboard. You set noindex in code, headers in config, and status codes in hosting rules. That gives you precise control, and makes it easy to ship the wrong rule to production.

Next.js App Router: the Metadata API

In the App Router, set robots metadata with the generateMetadata and metadata exports. For a page that should be excluded:

// app/thank-you/page.js
export const metadata = {
  robots: {
    index: false,
    follow: true,
  },
};

Next.js renders this as a robots meta tag in the head. For dynamic routes, use generateMetadata and return a robots object based on the data, for example hiding items flagged as unpublished or expired. Confirm in the output HTML that the tag is present in the initial response and not only after hydration.

Pages Router

In the Pages Router, add the tag in the page's head with the next/head component:

<Head>
  <meta name="robots" content="noindex" />
</Head>

Server-render or statically generate the page, so the tag is in the initial HTML.

Sending X-Robots-Tag in next.config

For file types, path patterns or a whole host, set a response header with the headers option in next.config.js:

// next.config.js
module.exports = {
  async headers() {
    return [
      {
        source: '/downloads/:path*',
        headers: [{ key: 'X-Robots-Tag', value: 'noindex' }],
      },
    ];
  },
};

Headers defined this way apply to responses served by Next.js. If you deploy behind a CDN that overrides headers, or export a fully static site, use the hosting platform's header configuration instead. See the X-Robots-Tag guide.

Static site generators and hosts

The same principle applies to Hugo, Jekyll, Eleventy, Astro and plain HTML: emit a robots meta tag from a template or front matter flag, and use host-level header rules for files.

WhereTypical way to noindex
Template or layoutOutput the meta tag when a front matter flag such as noindex: true is set
Netlify-style hostingA headers file or config rule for paths
Cloudflare Pages-style hostingA headers file with per-path rules
Vercel-style hostingHeaders in the project configuration
Apache or NginxDirectives shown in the header guide

File names and syntax differ per host, so use your host's current documentation for the exact format.

Status codes: 404 and 410

In Next.js, calling notFound() in a server component renders the not-found UI with a 404 status. For a 410 or custom redirects, use middleware, route handlers or hosting rules. For static exports, deleting the file results in a 404 from the host. A custom 404 page must return the 404 status; a design that returns 200 with "not found" text is a soft 404. See 404 vs 410.

For permanent redirects, the redirects() function in the config returns a 308 or 301 depending on the permanent option. Google's redirect documentation describes which status codes are treated as permanent. Redirect only to a relevant replacement.

Environments: the production safety net

Most real incidents here are environment mistakes. Control noindex through configuration, not hand-edited lines:

// Example: noindex everywhere except production
const isProd = process.env.DEPLOY_ENV === 'production';
export const metadata = {
  robots: isProd ? { index: true, follow: true } : { index: false, follow: false },
};

More on this in staging and dev sites.

Client-side rendering warning

If a page's robots tag is added only by client-side JavaScript, the initial HTML contains no directive. Google processes JavaScript, but a directive in the initial HTML or header is the dependable approach. Prefer server rendering or static generation for any page that must be excluded, and verify with a live test in URL Inspection.

Sitemaps

If you generate a sitemap from your routes, filter out noindexed routes. In the App Router, a sitemap.js file can return only the URLs you want. A sitemap that lists excluded pages is a mixed signal. See the sitemaps overview.

Verify

  1. Build and deploy. Run curl -sI and curl -s | grep -i robots against the live URL.
  2. Check for duplicate robots tags from nested layouts. Metadata merges across layouts in the App Router, so check the final output.
  3. Confirm robots.txt does not block the URL.
  4. Inspect in Search Console, request indexing, and track the status. See how to verify a page is deindexed.

A realistic rollout plan

Treat indexing rules as code that deserves tests. Add a small script to your CI pipeline that fetches a list of critical URLs from the preview build and asserts two things: the pages you want indexed contain no noindex directive, and the pages you want excluded do contain one. Keep the list in version control next to the routes it protects. When someone adds a new thank-you page or an expired campaign route, the checklist reminds them to add it to the excluded list and to remove it from the sitemap generator.

Finally, remember that removal still depends on a recrawl. After deploying, use URL Inspection on a few representative URLs, and record the date. If a page is sensitive and cannot wait, file a temporary removal while the permanent change propagates.

Frequently asked questions

How do I add noindex to a Next.js page?

In the App Router, export a metadata object with robots index false, or return it from generateMetadata. In the Pages Router, add a robots meta tag with next/head.

How do I send X-Robots-Tag for PDFs in Next.js?

Use the headers function in next.config.js with a source pattern, or configure your host if the site is statically exported.

Does notFound() return a 404 status?

It renders the not-found UI with a 404 status in server components. Verify with curl on your deployed site.

Should I use robots.txt on static hosting?

Only for crawl control. For removal use noindex headers, errors, or authentication.