| 1 |
Usage |
| 2 |
##### |
| 3 |
|
| 4 |
The extension relies on the ``redirects`` configuration option in ``conf.py``. ``redirects`` option maps a *source* to a *target*. |
| 5 |
|
| 6 |
.. highlight:: python3 |
| 7 |
|
| 8 |
:: |
| 9 |
|
| 10 |
redirects = { |
| 11 |
"<source>": "<target>" |
| 12 |
} |
| 13 |
|
| 14 |
By default, ``redirects`` is empty (i.e. generates no redirect files). |
| 15 |
|
| 16 |
*Source* (the key in ``redirects`` map) is a *docname*, i.e. document path without a suffix (an extension). Most Sphinx projects use ``.rst`` as extension. For example, a docname for the file ``index.rst`` is ``index``, for ``agents/intro.rst`` is ``agents/intro``, etc. |
| 17 |
|
| 18 |
Source may be non-existing document or existing document. If source does not exist, |project| creates new redirect .html file. For existing document, document's .html file will be overwritten with redirect file. To select multiple existing documents, a `source wildcards <Wildcard syntax_>`_ can be used. |
| 19 |
|
| 20 |
*Target* (the value in ``redirects`` map) is an URL that will be used in the HTML redirecting file. It may be specified using the placeholder to reuse source docname (see `Target placeholders`_). |
| 21 |
|
| 22 |
Target value must correspond to the output file naming of chosen Sphinx builder. For example, html builder creates ``docname.html``, while dirhtml ``docname/index.html``. |
| 23 |
|
| 24 |
.. important:: The extension works only for HTML-based builders like html and dirhtml. When building to other outputs (linkcheck, latex), it does nothing. |
| 25 |
|
| 26 |
The redirect is relative to the current page. So, if you want to redirect ``guides/index.html`` to ``index.html`` use: |
| 27 |
|
| 28 |
.. highlight:: python3 |
| 29 |
|
| 30 |
:: |
| 31 |
|
| 32 |
redirects = { |
| 33 |
"guides/index": "../index.html" |
| 34 |
} |
| 35 |
|
| 36 |
Redirect old documents |
| 37 |
********************** |
| 38 |
|
| 39 |
The basic usage is to redirect document you delete or rename to a new file within the same project. For example, if you rename ``setup.rst`` to ``install.rst``:: |
| 40 |
|
| 41 |
redirects = { |
| 42 |
"setup": "install.html", |
| 43 |
} |
| 44 |
|
| 45 |
Newly created ``setup.html`` will contain ``<meta http-equiv="refresh" content="0; url=install.html">``. |
| 46 |
|
| 47 |
Or, if you output to dirhtml, target must be a folder:: |
| 48 |
|
| 49 |
redirects = { |
| 50 |
"setup": "install/", |
| 51 |
} |
| 52 |
|
| 53 |
.. rubric:: Absolute URLs |
| 54 |
|
| 55 |
The target maybe any value. For example, if particular page is now on the different website:: |
| 56 |
|
| 57 |
redirects = { |
| 58 |
"install/requirements": "https://anotherwebsite.com/docs/requirements.html", |
| 59 |
} |
| 60 |
|
| 61 |
It will create ``install`` folder (if it does not exist) and ``requirements.html`` containing ``<meta http-equiv="refresh" content="0; url=https://anotherwebsite.com/docs/requirements.html">``. |
| 62 |
|
| 63 |
Redirect existing documents |
| 64 |
*************************** |
| 65 |
|
| 66 |
Source (the key in the ``redirects`` option) may be specified with wildcards. If wildcard is used, it means that you want to expand it against existing documents. |project| creates redirect .html file that will overwrite original document's html file. |
| 67 |
|
| 68 |
For example, if all FAQ documents in ``faq/`` folder should be redirected to dedicated forum website, but you don't want to delete them in your documentation:: |
| 69 |
|
| 70 |
redirects = { |
| 71 |
"faq/*": "https://website.com/forum/faq", |
| 72 |
} |
| 73 |
|
| 74 |
Now, html files originally produced by documents in ``faq/`` like ``supported-os.html``, ``licencing.html``, etc., are all replaced with body ``<meta http-equiv="refresh" content="0; url=https://website.com/forum/faq">`` |
| 75 |
|
| 76 |
Wildcard syntax |
| 77 |
=============== |
| 78 |
|
| 79 |
Wildcards for selecting existing source documents know only ``*``, ``?``, ``[seq]`` and ``[!seq]`` patterns. |
| 80 |
|
| 81 |
* ``*`` matches everything, while ``?`` any single character. |
| 82 |
* ``[seq]`` matches any character in the seq, ``[!seq]`` any character not in seq. |
| 83 |
|
| 84 |
(If you are curious, matching is using Python3 `fnmatch() <https://docs.python.org/3/library/fnmatch.html>`_.) |
| 85 |
|
| 86 |
.. important:: In other words, for example ``**`` used as "recursive" has no meaning here. |
| 87 |
|
| 88 |
Target placeholders |
| 89 |
******************* |
| 90 |
|
| 91 |
Matched document in the source, is available in the target as ``$source`` or ``${source}`` placeholder. Because source notation (a docname) is without suffix, you may need to append ``.html`` or ``/`` suffix after the placeholder. |
| 92 |
|
| 93 |
For example, if all FAQ documents in ``faq/`` folder should be redirected to dedicated forum website with the identical filenames in URL, but you don't want to delete them in your documentation:: |
| 94 |
|
| 95 |
redirects = { |
| 96 |
"faq/*": "https://website.com/forum/faq/$source", |
| 97 |
} |
| 98 |
|
| 99 |
Now, html files originally produced by documents in ``faq/`` like ``supported-os.html``, ``licencing.html``, etc., have replaced bodies like ``<meta http-equiv="refresh" content="0; url=https://website.com/forum/faq/supported-os">``, etc. |
| 100 |
|
| 101 |
Redirect everything |
| 102 |
******************* |
| 103 |
|
| 104 |
Occasionally, you have to move complete documentation to a new home. It's easy with wildcard and placeholder:: |
| 105 |
|
| 106 |
redirects = { |
| 107 |
"*": "https://anotherwebsite.com/docs/$source.html" |
| 108 |
} |
| 109 |
|
| 110 |
.. tip:: To help search engines to understand the transfer, update (or set) `html_baseurl <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_baseurl>`_ option to the new website, too. |