Search code examples
url-routingpython-sphinx

How to make Sphinx resolve URL links from e.g. /about-manual to /about-manual.html


I'm creating a Sphinx documentation and I struggle to identify the "proper" way to set up the structure and links.


STRUCTURE 1#

Currently, my structure looks as below:

index.rst
   about-manual/index.rst

Inside my root index.rst, the toctree is as below:

===========================
Contents
===========================

.. toctree::

   about-manual/index

This results in the below links:

https://example.com/docs/             --> Content of index.rst
https://example.com/docs/about-manual --> Content of about-manual/index.rst
  • This works as intended in terms of link resolution
  • However, I'm unsure if it's the "proper" way of setting up my Sphinx structure

STRUCTURE 2#

index.rst
about-manual.rst

Inside my root index.rst, the toctree is as below:

===========================
Contents
===========================

.. toctree::

   about-manual

This results in the below links:

https://example.com/docs/                  --> Content of index.rst
https://example.com/docs/about-manual      --> ERROR
https://example.com/docs/about-manual.html --> Content of about-manual.rst
  • This results in a more compact/simple Sphinx structure
  • However, if a user enters an URL without explicit .html at the end, the link is broken

Am I missing a basic configuration setting in Sphinx in order to make the link resolution work as per my expectation with 'STRUCTURE 2#' - without having to add the explicit .html at the end?

And is it possible to avoid having the Sphinx documentation explicitly resolve to the index.html at the end of an URL path? It does this as expected on the root index, but in 'STRUCTURE 1#' all sub pages explicitly show the index.html at the end.

I've looked at html_file_suffix and html_link_suffix, but I've not been able to make these work for my purpose either.


Solution

  • I found a solution for this by using sphinx-build -b dirhtml instead of sphinx-build -b html (as proposed by Jesse Tan from the sphinx_rtd_theme team) - for details see this link.

    It basically lets me use the approach in 'STRUCTURE 2#', but it builds the output with an index.html files structured in folders named as per the .rst files.

    Importantly, the internal links are also updated to not include the index.html part.