noindex, nofollow and hreflang From a Crawler's Side: Independent Rules and a Better Language Signal

By Lars Nielsen ·

SEO guides explain noindex and hreflang from the website's side: what to put in your pages so search engines treat them right. Building a crawler for my own search engine meant implementing them from the other side, and two July changes taught me more about both tags than the guides had. One was a tag my crawler had simply ignored; the other was a tag I started trusting more than the one meant for the job.

noindex: a rule my crawler never read

The crawler respected robots.txt from the start. But robots.txt only says which URLs may be fetched. A page can also say, in its own HTML, that it doesn't want to be in search results, with a robots meta tag, and the indexer had never looked for one. Pages that asked not to be indexed were indexed anyway.

The fix read <meta name="robots"> and implemented the two directives the way Google's documentation defines them, as independent rules:

  • noindex: don't show this page in search results. The page was still parsed and its links were still followed for discovery, because the directive says nothing about links. It just never got a document in the search index.
  • nofollow: don't follow the links on this page. The page was still indexed, but its outgoing links were never fed into discovery.
  • none: which Google documents as equivalent to noindex, nofollow.

The easy mistake is to treat one as implying the other, for example to skip a noindex page entirely. That throws away links the site never asked you to ignore. One of the five tests added with the change checks the opposite trap: that an explicit index, follow isn't misread as the negative directives just because it contains the same words.

Here is the rule as a self-contained PHP 8.4 function using the new HTML parser. It differs from the indexer's original in one way: the original treated content="none" as an exact match, so none, noarchive would have slipped through, while this version splits the directives on commas first.

// Read the generic robots meta tags of an HTML page. noindex and nofollow are
// independent; "none" means both. Several tags combine.
function robotsMeta(string $html): array
{
    $doc = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
    $noindex = $nofollow = false;
    foreach ($doc->getElementsByTagName('meta') as $meta) {
        if (strtolower($meta->getAttribute('name') ?? '') !== 'robots') {
            continue;
        }
        $directives = array_map('trim', explode(',', strtolower($meta->getAttribute('content') ?? '')));
        $none = in_array('none', $directives, true);
        $noindex = $noindex || $none || in_array('noindex', $directives, true);
        $nofollow = $nofollow || $none || in_array('nofollow', $directives, true);
    }
    return ['noindex' => $noindex, 'nofollow' => $nofollow];
}

It passes eight cases, including that one, index, follow, two separate tags that add up to both directives, and a googlebot-only tag that it correctly ignores.

A third, less obvious decision: a noindex page still got its quality score and category. The search engine had a "discussed on" feature showing forum threads that discuss a result, and a forum page can be noindex and still be a real discussion of something. Not showing a page is a different thing from not knowing about it.

hreflang: a better language signal than lang

The second change came from a language problem. The search engine prioritised Danish content, and decided each site's main language by a majority vote over its pages. On a multi-language site, a single early page could swing that vote: the example that showed it was a license-deed page in Danish on a large international site, which happened to be crawled first.

The page language came from <html lang>, which is often set once in a site-wide template and never checked per page. A better signal turned out to be hreflang, used the way sites use it for translations: <link rel="alternate" hreflang="da" href="...">. Google's documentation says each language version must list itself as well as all the others. So a page that has an hreflang link pointing at its own URL is making an explicit, per-page statement about its own language. When the indexer finds that self-reference, it overrides <html lang>. The match is made by resolving the link to the same stored URL, not by comparing strings, so trivial differences in how the URL is written don't break it.

There's one value to filter out. hreflang="x-default" isn't a language: Google's documentation describes it as the fallback page for unmatched languages, such as a language selector. Taken literally, it would have put a language called x-default into the index. The value is now checked against the list of language codes the engine knows before it's accepted.

An irony worth noting: Google's own documentation says Google uses neither hreflang nor the HTML lang attribute to detect a page's language, but its own algorithms. For a small engine without that kind of language detection, an explicit self-declaration from the site was the most trustworthy signal available.

What generalises

If you build anything that reads web pages at scale, treat the page-level signals as separate rules, not hints that imply each other, and validate their values, because real pages use every value the specification allows and some it doesn't. And if you run a site: robots.txt controls whether a page is fetched, and the robots meta tag controls whether it's indexed. They do different jobs, and a crawler can only read the meta tag on a page it was allowed to fetch.

Leave a comment

Comments are reviewed before they appear.

Basic HTML allowed: links, emphasis, code and lists. Web addresses become links automatically.