Updated 2026-11-21

Sphinx SEO: meta descriptions, Open Graph, canonical URLs and the sitemap

Sphinx writes the page title and, once you set html_baseurl, a canonical link. Everything else a search result or a link preview needs comes from your sources or from two extensions: sphinxext-opengraph for Open Graph tags and fallback descriptions, and sphinx-sitemap for sitemap.xml. This guide covers what each one writes and where its defaults go wrong. For structured data, see JSON-LD for Sphinx.

Tested on Sphinx 9.2 with sphinxext-opengraph 0.13 and sphinx-sitemap 2.9, with the html and dirhtml builders: every snippet below was built and the output checked.

1. html_baseurl and the canonical link

# conf.py
import os
html_baseurl = os.environ.get("READTHEDOCS_CANONICAL_URL", "https://docs.example.com/")

2. Meta descriptions

Write one per page with the meta directive, near the top of the file:

Install
=======

.. meta::
   :description: Install the Example CLI on macOS, Linux and Windows.

That writes <meta content="…" name="description"> (attributes in that order, so a grep for name="description" content= misses it; the check in section 6 handles both).

Pages without one get a description from sphinxext-opengraph (ogp_enable_meta_description, on by default): the start of the page text, admonitions skipped, cut at 200 characters with ..., often mid-word. Two things to know:

3. Open Graph and X tags with sphinxext-opengraph

# conf.py
extensions = ["sphinxext.opengraph", "sphinx_sitemap"]
ogp_site_url = html_baseurl
ogp_image = "https://docs.example.com/_static/og-default.png"
ogp_description_length = 155
ogp_custom_meta_tags = [
    '<meta name="twitter:card" content="summary_large_image" />',
]

Every page then gets og:title, og:type, og:url, og:site_name, og:description, og:image and og:image:alt.

4. Keeping pages out of search

.. meta::
   :robots: noindex

That writes <meta content="noindex" name="robots">, but sphinx-sitemap still lists the page, so add it to sitemap_excludes too (next section).

5. sitemap.xml and robots.txt

# conf.py
sitemap_url_scheme = "{link}"
sitemap_excludes = ["search.html", "genindex.html", "guide/old.html"]   # html builder
# sitemap_excludes = ["search/", "genindex/", "guide/old/"]            # dirhtml builder
html_extra_path = ["_extra"]   # _extra/robots.txt is copied to the site root

6. Check the build

sphinx-build -b dirhtml docs _build/dirhtml
grep -rLE --include='*.html' 'name="description"' _build/dirhtml
grep -rhoE --include='*.html' 'property="og:description" content="[^"]*"' _build/dirhtml | sort | uniq -c | sort -rn | head
grep -rl --include='*.html' 'content="[^"]*\.\.\." name="description"\|name="description" content="[^"]*\.\.\."' _build/dirhtml | wc -l
cat _build/dirhtml/sitemap.xml

The first lists pages without a description, the second shows shared preview texts, the third counts pages whose description is a truncated fallback. Duplicate titles and descriptions on a docs site goes through fixing those. To see the result as Google and the link previewers do, run a few deployed pages through the free SEO checker, or up to 50 pages from your sitemap through the site checker.

Automating it with Headlark

Writing a meta directive and an :og:description: field for each of hundreds of pages is the slow part, and the fallback is a cut-off first paragraph. Headlark's build step runs after sphinx-build on the HTML output and writes those tags from each page's content. It's a Node.js CLI, so the build environment needs Node 20 or newer next to Python; GitHub Actions, Netlify and Cloudflare Pages all have it, and on Read the Docs you can install it in a build.jobs step. Add it once with npm i -D headlark, then:

sphinx-build -b dirhtml docs _build/dirhtml
npx headlark _build/dirhtml --site-url https://docs.example.com/

Use the same address as html_baseurl. Set HEADLARK_KEY in the build environment.

Try it

Try it free for 14 days on one site and up to 500 pages, no card needed. Add your site, copy its secret key, and your next build ships with SEO.