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#
In the project’s
conf.py
file, specify thehtml_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 ishttps://writing-technically.readthedocs.io/en/latest/
.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"
:canonical: https://writing-technically.readthedocs.io/en/latest/index.html
Build the site.