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/")
- Sphinx writes
<link rel="canonical">on every page oncehtml_baseurlis set, and nothing without it. Keep the trailing slash. - The
htmlbuilder's canonicals end in.html, the home page's too:https://docs.example.com/index.html. If your server or host serves clean URLs (/guide/install), the canonical points at a different address than the one people share. Thedirhtmlbuilder writes/guide/install/and/instead; pick the builder whose URLs match what you serve. - On Read the Docs,
READTHEDOCS_CANONICAL_URLholds the address of the version being built, so the line above works there and locally.
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:
- Pages with a toctree get the toctree entries appended. Our test home page's fallback ended in "Install, Old page." Give index pages a written description.
- Set
ogp_description_length = 155so the fallback fits a search result instead of being cut again by Google.
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.
og:descriptionignores your meta description. It is always built from the page text, so a page with a hand-written.. meta:: :description:still shares a truncated first paragraph. Add a field at the very top of the file (before the title) to set it:
The field list is consumed by the extension and doesn't show on the page.:og:description: Install the Example CLI on macOS, Linux and Windows. Install =======og:typeiswebsiteon every page.ogp_typechanges it for all pages, the home page included, so leave it unless every page is an article.- No
twitter:cardis written; theogp_custom_meta_tagsline above adds it, so X shows the large image. - Images:
ogp_imageis the default for every page; use an absolute URL to a PNG or JPG around 1200×630.ogp_social_cardscan generate a card per page but needs matplotlib in the build environment; without it, the build only logs "matplotlib is not installed, social cards will not be generated" and usesogp_image. If a card still has no picture, og:image not showing covers the other causes.
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
sitemap_url_scheme = "{link}": the default scheme is{lang}{version}{link}, and Sphinx setslanguagetoenby default. Withversion = "2.1"inconf.py(most projects have one), the sitemap listshttps://docs.example.com/en/2.1/guide/install/, which 404s unless you really serve versioned folders under the base URL. Read the Docs already puts language and version inhtml_baseurl, so{link}is right there too.sitemap_excludesmatches the builder's link form. Withdirhtml,"guide/old.html"excludes nothing; use"guide/old/". Leave outsearchandgenindex, which are otherwise listed.- robots.txt: Sphinx doesn't write one. Put it in a folder listed in
html_extra_path, with aSitemap: https://docs.example.com/sitemap.xmlline.
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.
- Each page gets a title, a description, Open Graph and X tags written from its own content, and alt text for images that have none.
- An existing
og:image, fromogp_imageor a social card, is kept. Headlark doesn't create images. - Your canonical, robots tags and JSON-LD types are kept.
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.