Canonical and hreflang: checking reciprocal links across three languages

Canonical and hreflang in generated Symfony HTML and a separate Python rig: available translations, absolute URLs, self-canonical, and reciprocal links.

Language versions have their own addresses and consistent relationships with the other translations.

Language versions can exist and load successfully while describing their relationships differently. The Ukrainian page names the English page as an alternative, the English page points to an old address, and the Russian canonical accidentally points to Ukrainian text. Each page looks plausible in isolation. The contradiction appears when we examine the material as a group of translations.

This calls for a small matrix of available URLs and checks against actual responses. Consider an independent journal with uk, ru, and en locales: one article has its own address for every ready translation. This is a publication policy that can be checked before applying it to a real application.

Two relationships with different purposes

rel="canonical" names the preferred primary address among duplicate or very similar representations. RFC 6596 defines this relationship. The link does not perform a redirect: the browser can remain on the current page.

hreflang describes language or regional alternatives of the material. Google's guidance on localized versions calls for absolute addresses, self references, and reciprocal links between versions. In our journal, a ready translation has its own canonical and belongs to a shared set of alternatives.

This model does not make every language page compete for one Ukrainian address. It is also not a universal rule for regional copies in the same language. Two nearly identical English pages would need a different model for choosing a preferred URL. First establish which pages are actual translations of the same material.

Start with a list of ready pages

The matrix should come from the publication state of each translation. A language prefix in the routes does not establish that the text exists. A database record might also be a draft, a hidden version, or an empty placeholder.

For this example, a ready translation has nonempty content, an allowed public state, and a final address. For one article, that produces this independent list:

{
  "uk": "https://journal.example/uk/notebook",
  "ru": "https://journal.example/ru/notebook",
  "en": "https://journal.example/en/notebook"
}

The addresses are synthetic. What matters is that the list belongs to the article, rather than being created by mechanically replacing the first segment of its current URL. Real translations can have different slugs. After one is renamed, substituting a prefix can easily produce an address that never existed.

The identity of the material connects its translations; URLs describe their current locations. If we store only addresses without that relationship, a slug change starts to resemble the arrival of a new, unrelated article.

The same matrix in every head

For the Ukrainian page, the set looks like this:

<link rel="canonical" href="https://journal.example/uk/notebook">
<link rel="alternate" hreflang="uk" href="https://journal.example/uk/notebook">
<link rel="alternate" hreflang="ru" href="https://journal.example/ru/notebook">
<link rel="alternate" hreflang="en" href="https://journal.example/en/notebook">

In the English head, the canonical changes to the English address. The alternate set stays the same. Every page receives it from the same prepared matrix, so templates do not have to guess their neighbours independently.

The hreflang attribute concerns the language of the target resource; the HTML Standard defines its place on link. We use uk for Ukrainian here. The local check accepts only the agreed uk, ru, and en set; it does not claim to validate every possible language tag.

The exercise also compares html lang with the record's locale. This checks the consistency of our HTML, rather than determining how a search engine identifies the actual language of the text. An incorrect English attribute on Ukrainian material needs fixing even when the alternative addresses are correct.

Check reciprocity through HTTP

Comparing template arrays is useful, but it does not reveal the route's response. An address might return a redirect, an error, or an entirely different document type. The check therefore starts by fetching every URL included in the agreed matrix.

In this journal, an included address must immediately return 200 and HTML. The checker does not follow redirects automatically. If the old English address returns 301, that is precisely what we want to see, rather than silently checking another page and approving an outdated link.

Next, check for one canonical per page, exact target URLs, no duplicate hreflang for the same locale, and a return link. Comparison is exact: it does not arbitrarily remove a trailing slash, a query, or path case differences. Such transformations belong to routing rules, not the checker's guesses.

A complete, identical set of alternatives is the chosen policy for our small cluster. Its advantage is that an update can be checked as a whole. This does not claim that every incomplete network of language links is always unusable for search.

A missing translation means a smaller cluster

Suppose the Ukrainian and Russian texts are ready while the English translation is still being edited. The matrix then contains only uk and ru. Both pages refer to themselves and to each other. The exercise also checks that the undeclared English address is never requested.

Do not temporarily serve Ukrainian text at the English address and describe it as a ready English version. That conceals the missing translation. A link to a draft returning 404 does not help a reader move between languages either.

Once the English translation is accepted, update the shared matrix and check every included page. If the head is cached separately from the body, account for that cache as well. Updating one record alone does not establish that existing pages already show the new alternative set.

When canonical and noindex contradict the plan

For ready translations in this example, we expect a self-canonical. If the English canonical points to Ukrainian, the checker flags a conflict with our model. Google's canonical documentation helps distinguish a preferred-address signal from the search engine's actual choice. Correct markup does not guarantee that this particular URL will be selected.

Another contradiction is a page included in the public matrix that carries noindex. Check both supported meta rules and the applicable X-Robots-Tag. These mechanisms are described in the documentation for robots meta and HTTP headers. A permissive rule in one place must not conceal a restriction elsewhere.

The exercise supports generic rules and googlebot rules in the declared simple format. It does not implement the complete robots grammar or read robots.txt. Finding no noindex therefore means only that the received response contains no supported exclusion.

An independent rig with deliberate defects

The Python exercise uses no CMS and makes no external requests. A temporary server listens only on loopback; logical journal.example addresses map to its local routes. We fetch real HTTP responses and parse HTML, without contacting the public journal.

The Python standard library HTTP server is a tool for this rig. This is not a proposal to use it for production traffic. Responses are limited to 65536 bytes; an oversized document is not truncated into an apparently valid head but produces a separate read error.

Condition What the check should detect
Three ready translations A consistent complete set without findings
Only two ready translations A consistent smaller set without a fictional address
Canonical in another language Conflict with the agreed address
Missing return link An incomplete relationship between versions
301 or 404 An unsuitable current address
Meta or HTTP noindex Conflict with inclusion in the matrix
Duplicate tag or relative URL Ambiguous or incomplete markup

The executed exercise contained 14 scenarios: 2 consistent clusters and 12 deliberate defects. Both correct sets produced no findings, and each defect was detected by its expected check. We also confirmed that a redirect did not conceal the old address. This is the result of one particular rig, not a website indexing check.

Native head generation in the same LocaleRead

Separately extend the earlier LocaleRead module in one article_demo on Symfony 8.1 and PHP 8.4+. SeoLinks adds trusted origin https://journal.example to its own root-relative reader path; SeoCluster includes only available translations and a self-canonical. The origin comes from trusted configuration rather than an arbitrary HTTP header. Earlier API payloads and relative ArticleLinks remain unchanged.

The JSON and HTML excerpts above belong to the unchanged Python experiment with synthetic /uk/notebook addresses. Native HTML uses the existing /locale-demo/{_locale}/read/{slug} routes and localized slugs. These are two separate experiments with the same policy rather than a check of native responses against the old static fixture.

<?php

declare(strict_types=1);

namespace App\LocaleRead;

final readonly class SeoCluster
{
    public function __construct(private FixtureCatalog $catalog, private SeoLinks $links)
    {
    }

    /** @return array{canonical: string, alternates: array<string, string>} */
    public function forArticle(LocalizedArticle $selected): array
    {
        $alternates = [];

        foreach ($this->catalog->available($selected->articleId) as $translation) {
            $alternates[$translation->locale] = $this->links->forTranslation($translation);
        }

        $canonical = $alternates[$selected->locale] ?? null;

        if ($canonical === null || $canonical !== $this->links->forTranslation($selected)) {
            throw new \LogicException('The selected translation must belong to the published cluster.');
        }

        return ['canonical' => $canonical, 'alternates' => $alternates];
    }
}

Add these two entries under the existing services block, preserving earlier configuration.

services:
    App\LocaleRead\SeoLinks:
        arguments:
            $origin: 'https://journal.example'
    App\LocaleRead\SeoCluster: ~

For tests only, extend config/services_test.yaml: the public fixture service permits replacing the catalogue for the two-language scenario. Preserve other test services.

services:
    App\LocaleRead\FixtureCatalog:
        autowire: true
        public: true
        arguments:
            $fixturePath: '%kernel.project_dir%/fixtures/locale-catalog.json'

The native check contains 13 test cases and 111 assertions: 5 HTTP cases and 8 unit cases. It checks self-canonical and the complete reciprocal set of published translations in generated HTML. The historical Python14/14 result is bound to four unchanged runtime inputs and was not rerun for this PHP addition. Neither result proves Google indexing, a sitemap, or cross-origin deployment.

What to check after a URL changes

The list of ready locales, routes, canonicals, and alternatives must change consistently. After a slug change, fetch the other language versions as well as the new page. The old URL may redirect readers correctly, but current alternates should point to the agreed final address.

For a real application, the next step is to run this kind of check against its controlled environment, accounting for access and its URL policies. Check sitemap, HTTP Link, JavaScript markup, regional variants, and x-default separately when they are used. They are outside this exercise.

A useful result is a reproducible list of contradictions naming the affected page and the reason. It lets us fix head generation or a translation's state before publication. Actual crawling, the selected canonical, and appearance in search still require their own observations afterwards.

  1. Symfony Core The product owns the route: changing URLs without rewriting the capability
  2. API API languages and links: one article across three routes
  3. SEO The page exists but is not indexed: evidence-based diagnosis