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.pyfile, specify thehtml_baseurloption 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
canonicaltag 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.htmlBuild the site.