Technical SEO

X-Robots-Tag: How to Noindex PDFs and Files the Headers Way

By , Lead SEO & Co-founder 6 min read
A file server room at night where document trays slide out of a rack, each stamped by a small glowing seal without lettering, a cyan beam reading the seal and one crimson seal marking a file to hide

The x-robots-tag is an HTTP header (a short note the server sends along with a file, before the content itself) that gives Google the same instructions as a robots meta tag. The difference is where it lives. A meta tag needs an HTML page to sit in. A PDF, a Word document or a product image has no <head>, so the header is the only way to say "don't index this."

That's the whole reason it exists for most small sites. Your old price list PDF ranks above your pricing page. A supplier spec sheet outranks your product page. A staff handbook somehow shows up for your brand name. The fix is one line on the server.

If you'd rather not touch server files yourself, tell me what's showing up in Google and I'll look at it. Otherwise, the snippets below are ready to paste.

What the X-Robots-Tag header looks like

When a server sends a file, it adds headers first. With the rule in place, a PDF response starts like this:

Text
HTTP/1.1 200 OK
Content-Type: application/pdf
X-Robots-Tag: noindex

Google's documentation lists the rules the header supports, and they're the same ones you'd put in a meta tag. The ones that matter for files:

Rule What it does
noindex Keep this file out of search results
nofollow Don't follow links inside it (PDFs can contain links)
none Shorthand for noindex, nofollow
noimageindex Don't index images on the page
unavailable_after: 2026-12-31 Drop it from results after a date. Handy for event flyers

One rule you can skip: noarchive. Google says it's no longer used, because the cached-page link it controlled doesn't exist anymore. If your template still sends it, it's harmless clutter.

You can also aim a rule at one crawler, like X-Robots-Tag: googlebot: noindex. Rules without a crawler name apply to all of them. When rules conflict, Google applies the more restrictive one.

Noindex PDFs on Apache with .htaccess

This is the x-robots-tag htaccess version most people need. It goes in the .htaccess file in your site's root folder and needs mod_headers, which nearly every Apache and LiteSpeed host has switched on.

.htaccess / Apache
# Adds "noindex" to every PDF, Word and Excel file on the site.
# Could break: nothing visible to visitors. It only removes these files from search over time.
<IfModule mod_headers.c>
    <FilesMatch "\.(?:pdf|docx?|xlsx?)$">
        Header set X-Robots-Tag "noindex"
    </FilesMatch>
</IfModule>

The <IfModule> wrapper stops a 500 error if the module happens to be missing. Without it, a host with mod_headers turned off would take your whole site down over one line. (If that has already happened to you, here's how to recover from a broken .htaccess.)

The Header set directive replaces any earlier header of the same name, so you won't end up with two conflicting copies.

Only one folder of PDFs

Often you want the brochure indexed but not the internal forms. On WordPress, uploads live under /wp-content/uploads/, sorted by year and month, which makes path rules messy. The cleaner trick is a dedicated folder with its own small .htaccess:

.htaccess / Apache
# Put this .htaccess INSIDE the folder you want hidden, e.g. /private-docs/.
# Every file in that folder gets noindex. Could break: nothing, but it is not
# a password. Anyone with the link can still open the file.
<IfModule mod_headers.c>
    Header set X-Robots-Tag "noindex, nofollow"
</IfModule>

That last point deserves its own line. Noindex hides a file from search. It doesn't hide it from people. If a document is truly private, it needs a login, not a header.

The same rule on Nginx and Cloudflare

Nginx ignores .htaccess, so the rule goes in your server block:

Nginx
# Noindex for PDFs and Office files. Could break: other add_header lines
# in the server block stop applying to these files (see the note below).
location ~* \.(?:pdf|docx?|xlsx?)$ {
    add_header X-Robots-Tag "noindex" always;
}

There's a sneaky Nginx rule here. Headers set with add_header are inherited from the level above only if the current block has no add_header of its own. So the moment you add X-Robots-Tag in this location, your security headers set at the server level quietly stop applying to PDFs. Copy those lines into the block too.

On Cloudflare, a response header transform rule can add the header without touching the server. Match on the file extension ends with .pdf, then choose "Set static" with the header name X-Robots-Tag and the value noindex. This is useful on hosts that don't let you edit server files.

The robots.txt trap

This is the mistake I see most. Someone wants a folder of PDFs out of Google, so they block it in robots.txt and add the noindex header. Belt and braces, right?

It backfires. Google can only read the header if it's allowed to fetch the file. Google's own guidance says that if robots.txt blocks a resource, the crawler will never see the noindex rule, and the file can still appear in results when other pages link to it. Usually as a bare URL with no description, which looks even worse.

The fix order:

  1. Add the X-Robots-Tag header.
  2. Remove any robots.txt Disallow covering those files.
  3. Wait for Google to recrawl and drop them.
  4. Only then, if you really want, add the robots.txt block back.

Step 4 is optional. For most sites I leave it out, so a future "why is this indexed?" question has a clean answer.

When a canonical header beats noindex

Sometimes a PDF duplicates a real page: the printable version of a service page, or a menu that also exists as HTML. Noindex works, but there's a gentler option. Google supports a rel="canonical" HTTP header for non-HTML files like PDFs, which points Google at the HTML page as the preferred version.

.htaccess / Apache
# Tells Google the HTML page is the main version of this PDF.
# Could break: nothing. Google treats canonicals as a strong hint, not an order.
<IfModule mod_headers.c>
    <Files "spring-menu.pdf">
        Header set Link "<https://www.example.com/menu/>; rel=\"canonical\""
    </Files>
</IfModule>

Use this when the PDF has links or mentions worth consolidating into the page. Use noindex when the file has no business in search at all.

How to check the header is working

Don't trust the config, check the response:

Terminal
# Prints the response headers only. Look for the X-Robots-Tag line.
curl -sI https://www.example.com/files/price-list.pdf | grep -i x-robots-tag

Then paste the file URL into the URL Inspection tool in Google Search Console. A live test shows whether indexing is allowed and, if not, where the noindex came from. Expect the file to leave results only after Google recrawls it, which can take days or weeks for a file nobody links to much.

Vlad's take

In audits, indexed PDFs are one of the quietest problems I find. Nobody looks at them, they rarely change, and they can sit in the index for years competing with the pages you actually maintain. They're also a common slice of index bloat on WordPress sites, next to tag and attachment pages. The header fix takes five minutes. Finding which files are indexed is the slower part, and it's one of the checks in my crawl-and-indexing audit.

Final Thoughts

Use the x-robots-tag header for anything that isn't an HTML page: PDFs, Office files, images, feeds. Set it in .htaccess, the Nginx config or a Cloudflare rule, test it with curl, and never block the same files in robots.txt while you wait for Google to see it.

If you'd like a second pair of eyes on what Google has indexed from your site, you can request a technical audit here. Starting at $750 for sites under 100 pages, and I reply with a plan and a fixed quote within one business day.

X-Robots-Tag FAQs

How do I noindex a PDF?

Add the HTTP header X-Robots-Tag: noindex to the PDF's response. On Apache, wrap Header set X-Robots-Tag "noindex" in a <FilesMatch "\.pdf$"> block in .htaccess. On Nginx, use add_header in a location block that matches .pdf. Make sure robots.txt doesn't block the file, or Google never sees the header.

What's the difference between X-Robots-Tag and the robots meta tag?

They accept the same rules. The meta tag sits in an HTML page's <head>, so it only works for web pages. The X-Robots-Tag is sent as an HTTP header by the server, so it works for any file type, including PDFs, images and Word documents. It also lets you apply one rule to many files at once by pattern.

How long does it take for a noindexed PDF to leave Google?

It drops out after Google recrawls the file and sees the header. For a file that's linked often, that can be days. For an old PDF nobody links to, it can take weeks. Requesting indexing in the URL Inspection tool can speed up the recrawl for a few important files.

Can I use X-Robots-Tag in WordPress without editing server files?

Not with WordPress settings alone, because PDFs are served straight by the web server without loading WordPress. Your options are .htaccess (on Apache or LiteSpeed hosting), a Cloudflare response header rule if your site runs through Cloudflare, or asking your host to add the Nginx rule for you.

Want a second pair of eyes on your site?

Tell me what's going on in the technical seo audit questionnaire. I reply with a plan and a fixed quote within one business day.

Starting at $750 Start the questionnaire