Tuesday, November 24, 2026

Blocking PDFs from Google in Sitecore

Blocking Specific PDFs from Google in Sitecore: Why robots.txt is not the good idea and X-Robots-Tag is


Recently I had to a task to hide specific PDFs from Google Search. The PDF's are publicly accessible but they just don't want the content indexed. 

At first glance, that sounds like a simple task with a disallow rule in the robots.txt file. But first glances can be wrong.
Using robots.txt to remove or hide PDFs from Google is a common misconception in web development— in fact, it can even make it worse.

Why robots.txt Is the Wrong Tool (and X-Robots-Tag Is the Right One)

Crawling and indexing are different things. Google’s official specifications point us in the right direction and make sure we use the right tools to stop indexing.

robots.txt only controls crawling, not indexing.

Per Google’s Introduction to robots.txt, a Disallow rule only tells compliant crawlers not to fetch the URL. But, if internal pages or external sites link to that PDF, Google can still index the URL itself and display it in search results. It is even mentioned in bold that robots.txt is not a mechanism for keeping a web page out of Google.

PDFs can’t hold <meta> tags.

On a HTML page, you can add a tag <meta name="robots" content="noindex" /> into the <head> section.  A PDF document however does not have a html head so it cannot use this tag. It is recommended to use the X-Robots-Tag HTTP response header for non-HTML resources like PDFs, videos, and images in its Robots meta tag, data-nosnippet, and X-Robots-Tag specifications.

The real trap: combining robots.txt with noindex.

As Google warns in Block Search indexing with noindex, a noindex rule only works if Googlebot is allowed to crawl the URL. If you block a PDF in robots.txt, that access is actually denied and Googlebot will not request the file, and will not see your noindex header, so it will leave an already-indexed PDF sitting in search results. You could rely on the path it used to find it in the first place, but I wouldn't count on that.

I need to emphasize as a security side note that the X-Robots-Tag is a directive for search engines, not an access-control mechanism. The document is still available to everyone. 

So now we know: we do not block the access in the robots.txt file and we do want to add a header in the response.

This was implemented on a Sitecore (10.2) site with SXA and MVC. I'll cover that solution and explain later how this can be ported to a more modern setup with SitecoreAI.


The Sitecore 10.2 (.NET / SXA) Implementation

We want content authors to toggle search engine visibility per PDF (inside the Media Library) so we cannot use hardcoded rewrite rules or any other path-based solution. So we extend the pdf templates.

Step 1: templates

  1. Create a new base template - e.g.  _RobotsAwareMedia.
  2. Add a section (e.g., Indexing or SEO) with a Checkbox field named NoIndex.
    I would suggest to make the field Shared as blocking a PDF probably should apply across all languages and versions
  3. Add _RobotsAwareMedia to the template inheritance of both built-in Sitecore PDF media templates:
    • /sitecore/templates/System/Media/Unversioned/Pdf
    • /sitecore/templates/System/Media/Versioned/Pdf
    These are probably not yet in your serialization if you are using that - you might want to add them.


Step 2: a custom MediaRequestHandler

We can override Sitecore’s media request handler so that whenever a PDF is requested, it checks the NoIndex field and appends X-Robots-Tag: noindex, nofollow to the HTTP response.

Because this solution uses SXA, our handler inherits from  the SXA one. If you are on a non-SXA Sitecore solution, simply inherit from the default one. You can find the one you are currently using the web.config as you will see when we get to the configuration.

>using System;
using System.Web;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Resources.Media;

namespace XX.Foundation.RequestHandlers
{
    public class RobotsAwareMediaRequestHandler : Sitecore.XA.Foundation.MediaRequestHandler.MediaRequestHandler //Sitecore.Resources.Media.MediaRequestHandler 
    {
        protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Media media)
        {
            Item item = media?.MediaData?.MediaItem;
            if (item != null && string.Equals(media.MediaData.Extension, "pdf", StringComparison.OrdinalIgnoreCase) &&
                    MainUtil.GetBool(item["NoIndex"], false))
            {
                context.Response.AppendHeader("X-Robots-Tag", "noindex, nofollow");
            }

            return base.DoProcessRequest(context, request, media);
        }
    }
}

A few notes on the code. Appending the header before calling the base function is intentional as the base implementation sets cache/status headers and setting our header first guarantees it is attached before headers are sent. We check for the pdf extension, limiting the field lookup to PDFs, avoiding unnecessary overhead on other media items like images. 
Also note that we set both noindex and nofollow. Noindex keeps the PDF out of Google’s search index, while nofollow instructs crawlers not to follow links embedded inside the document.


Step 3: registering the handler (in Web.config)

Sitecore’s media handler is an IIS HTTP handler mapped to sitecore_media.ashx in Web.config under <system.webServer><handlers>, which means it cannot be patched via App_Config/Include files.

Replace the default SXA handler registration in Web.config with your custom handler:

<!--<add verb="*" path="sitecore_media.ashx" type="Sitecore.XA.Foundation.MediaRequestHandler.MediaRequestHandler, Sitecore.XA.Foundation.MediaRequestHandler" name="Sitecore.MediaRequestHandler" />-->
<add verb="*" path="sitecore_media.ashx" type="XX.Foundation.RequestHandlers.RobotsAwareMediaRequestHandler, XX.Foundation" name="Sitecore.MediaRequestHandler" />

Don't forget to check how your web.config changes make it to the live production site - might be xdt transformations...


How This Translates to Headless (SitecoreAI) with Next.js

If you are moving toward SitecoreAI (or already on it) with a Next.js frontend and Experience Edge, you no longer have a .NET Content Delivery server, IIS Web.config, or MediaRequestHandler to override.

Still, the core principle stays identical—only the enforcement layer moves from .NET to the Edge / Next.js delivery part.

First, there is an interesting SitecoreAI detail:

  • By default, Experience Edge blocks indexing on all media. When media is served from the default edge domain, it automatically gets a X-Robots-Tag: noindex.
  • Custom hostnames revert this, so when you configure a custom hostname which you probably do, Google can index your documents again as Experience Edge omits that global X-Robots-Tag: noindex header.

Once you use a custom media domain, you’re right back to the per-PDF control. We can use the same architecture, just in different technologies. We keep the exact same template design. The handler code that used to be in C# now is created in the Next.js implementation. Route the pdf request through the rendering host and add the exact same header there.


Things to verify in production environments

In real-world scenarios your Sitecore site and its http headers probably pass through multiple layers between your application and the Google bot. So you will need to check:

CDN Caching and Header Stripping

If a PDF was already public and cached by your CDN, toggling NoIndex and publishing in Sitecore won’t update the cached response at the CDN edge until the TTL expires or the URL is purged.
Also, verify that your CDN is configured to pass through origin X-Robots-Tag header instead of stripping or overwriting them.


WAF / Firewall Rules

Assuming your WAF is not blocking Google bot, check that it is also not stripping your newly created header. Make sure to test through all the layers and not directly to the Content Delivery server.

robots.txt and IIS <customHeaders>

Esnure your robots.txt has no Disallow rule for your pdfs. Also verify that the Web.config <customHeaders> and/or IIS URL Rewrite outbound rules do not interfere with your X-Robots-Tag values.

GET vs. HEAD Requests and 304 Not Modified

Sitecore’s sitecore_media.ashx handler uses verb="*", so both GET and HEAD hit DoProcessRequest. Always test with both because some intermediate layers might treat headers on HEAD and GET differently.
Sitecore caches media on disk and handles this inside base.DoProcessRequest, so keeping our append before the base ensures the header isn’t skipped on 304 Not Modified requests.

How to Verify It Works

Use any tool you want to fetch a pdf from your site and verify the headers in the response. They should contain the robots tag.


Conclusion

Blocking PDFs from Google isn’t a robots.txt problem - it’s an HTTP response header problem. And one that can rather easily be done in Sitecore.


No comments:

Post a Comment