Sub topic 13
Controlling indexing: noindex versus robots.txt
This chapter assumes you know how HTTP status codes work, can read basic HTML, and can follow short Python snippets. You should also have read the earlier chapter on robots.txt. Here you will learn why robots.txt and noindex solve different problems, and why using them together often backfires.
Two controls, two stages
Search engines work in stages. A crawler fetches a page, then the indexer decides whether to store it. Each control acts at one stage:
| Control | Stage it affects | What it says |
|---|---|---|
| robots.txt `Disallow` | Crawling | "Do not fetch this URL." |
| `noindex` | Indexing | "You may fetch this page, but do not store it in the index." |
Blocking a fetch is not the same as blocking indexing. A crawler can learn that a URL exists from a link on another site. If robots.txt blocks the fetch, the engine never reads the page, but it may still list the bare URL in results, usually without a description.
The noindex directive
You can place noindex in the page's HTML:
<meta name="robots" content="noindex, follow">For non-HTML files such as PDFs, send the same directive as an HTTP response header: X-Robots-Tag: noindex.
The follow value tells the engine it may still follow the page's links. The crawler can only act on noindex if it can read the page, because the directive lives inside the response.
The common mistake
Suppose you want a page removed from search results. You add noindex to the page, and you also add a Disallow rule for it in robots.txt. The crawler now skips the page and never sees the tag. The URL can remain in the index.
The fix is to remove the Disallow rule, let the crawler fetch the page, and keep the noindex tag. After the page drops out of the index, you can add the robots.txt block again if you also want to save crawl requests.
Checking a page yourself
This script fetches a URL and reports whether robots.txt allows the fetch and whether the page carries a noindex signal. It checks the header and the meta tag.
import re
from urllib.parse import urljoin
from urllib.request import urlopen
from urllib.robotparser import RobotFileParser
def check(url, agent="ExampleBot"):
rp = RobotFileParser()
rp.set_url(urljoin(url, "/robots.txt"))
rp.read()
if not rp.can_fetch(agent, url):
return "blocked by robots.txt: noindex cannot be seen"
with urlopen(url, timeout=10) as resp:
header = resp.headers.get("X-Robots-Tag", "")
html = resp.read().decode("utf-8", errors="replace")
meta = re.search(
r'<meta[^>]+name=["\']robots["\'][^>]*content=["\']([^"\']*)',
html,
re.I,
)
directives = (header + " " + (meta.group(1) if meta else "")).lower()
return "noindex" if "noindex" in directives else "indexable"
print(check("https://example.com/"))The regular expression expects name before content, so it misses tags written the other way round. A real checker would use an HTML parser.
Choosing the right tool
- Use
noindexwhen a page may be crawled but must not appear in search results, such as thank-you pages or internal search results. - Use robots.txt when you want to reduce load or keep crawlers out of sections that waste crawl budget.
- Use authentication, not either directive, for truly private content. Both are requests that well-behaved crawlers honor, and neither protects data.
Exercise
A page has <meta name="robots" content="noindex">, and robots.txt contains Disallow: for the page's path. The URL still appears in search results. Why, and what should you change?
Show answer
The Disallow rule stops the crawler from fetching the page, so it never reads the noindex tag. The engine can still list the URL because other pages link to it. Remove the Disallow rule so the crawler can fetch the page and see noindex. Once the page leaves the index, you may restore the block if you want.
Exercise
How would you keep a PDF out of search results when the file has no HTML in which to put a meta tag?
Show answer
Send an X-Robots-Tag: noindex HTTP response header with the file. The crawler must be allowed to fetch the PDF to see the header.