Adding canonical URLs to a Sphinx site

On this page

Adding canonical URLs to a Sphinx site#


17 March 2024

For search optimisation purposes, you might want to designate a URL as the legitimate, authoritative URL for a page. To do so for a Sphinx site, you must specify a base URL in the conf.py file, and then, for every page, add its URL in the front matter.

Steps#

  1. In the project’s conf.py file, specify the html_baseurl option for HTML output. The value of the option should be the root URL of the project. For example, for this website, the root URL is https://writing-technically.readthedocs.io/en/latest/.

  2. For every file, add a canonical tag as the very first tag in the frontmatter, as follows:

    "canonical": "https://writing-technically.readthedocs.io/en/latest/index.html"

  3. Build the site.