Skip to content

Commit

Permalink
Updates from review
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed May 26, 2022
1 parent 91a58a7 commit 0ce44d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
32 changes: 24 additions & 8 deletions docs/user/user-defined-redirects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You can set up redirects for a project in your project dashboard's :guilabel:`Re
.. contents:: Table of contents
:local:

Quick Summary
Quick summary
-------------

* Go to the :guilabel:`Admin` tab of your project.
Expand All @@ -23,18 +23,19 @@ Features

- By default, redirects are followed only if the requested page doesn't exist
(*404 File Not Found* error), if you need to apply a redirect for files that exist,
mark the :guilabel:`Force redirect` option. **This option is only available on some plan levels**.
mark the :guilabel:`Force redirect` option.
**This option is only available on some plan levels**.
Please ask support if you need it for some reason.
- :ref:`user-defined-redirects:page redirects` and :ref:`user-defined-redirects:exact redirects`
can redirect to URLs outside Read the Docs,
just include the protocol in ``To URL``, e.g ``https://example.com``.

Redirect Types
Redirect types
--------------

We offer a few different type of redirects based on what you want to do.

Prefix Redirects
Prefix redirects
~~~~~~~~~~~~~~~~

The most useful and requested feature of redirects was when migrating to Read the Docs from an old host.
Expand Down Expand Up @@ -66,7 +67,7 @@ Where ``en`` and ``latest`` are the default language and version values for your
which will prepend ``/$lang/$version/`` to all incoming URLs.


Page Redirects
Page redirects
~~~~~~~~~~~~~~

A more specific case is when you move a page around in your docs.
Expand All @@ -88,11 +89,20 @@ the ``/`` at the start of the ``From URL`` doesn't include the ``/$lang/$version
If you want to set redirects only for some languages or some versions, you should use
:ref:`user-defined-redirects:exact redirects` with the fully-specified path.

Exact Redirects
Exact redirects
~~~~~~~~~~~~~~~

*Exact Redirects* are for redirecting a single URL,
and take into account the full URL (including language & version).
taking into account the full URL (including language and version).

You can also redirect a subset of URLs by including the ``$rest`` keyword
at the end of the ``From URL``.

Exact redirects examples
^^^^^^^^^^^^^^^^^^^^^^^^

Redirecting a single URL
````````````````````````

Say you're moving ``docs.example.com`` to Read the Docs and want to redirect traffic
from an old page at ``https://docs.example.com/dev/install.html`` to a new URL
Expand All @@ -112,6 +122,9 @@ Your users query would now redirect in the following manner::
Note that you should insert the desired language for "en" and version for "latest" to
achieve the desired redirect.

Redirecting a whole sub-path to a different one
```````````````````````````````````````````````

*Exact Redirects* could be also useful to redirect a whole sub-path to a different one by using a special ``$rest`` keyword in the "From URL".
Let's say that you want to redirect your readers of your version ``2.0`` of your documentation under ``/en/2.0/`` because it's deprecated,
to the newest ``3.0`` version of it at ``/en/3.0/``.
Expand All @@ -131,6 +144,9 @@ Similarly, if you maintain several branches of your documentation (e.g. ``3.0``
``latest``) and decide to move pages in ``latest`` but not the older branches, you can use
*Exact Redirects* to do so.

Migrating your documentation to another domain
``````````````````````````````````````````````

You can use an exact redirect to migrate your documentation to another domain,
for example::

Expand All @@ -143,7 +159,7 @@ Then all pages will redirect to the new domain, for example
``https://docs.example.com/en/latest/install.html`` will redirect to
``https://newdocs.example.com/en/latest/install.html``.

Sphinx Redirects
Sphinx redirects
~~~~~~~~~~~~~~~~

We also support redirects for changing the type of documentation Sphinx is building.
Expand Down
43 changes: 21 additions & 22 deletions readthedocs/redirects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,12 @@ def get_redirect_path_with_status(
:param full_path: Is the full path including the language and version parts.
:param forced_only: Include only forced redirects in the results.
"""

# Small optimization to skip executing the big query below.
if forced_only and not self.filter(force=True).exists():
return None, None

def normalize_path(path):
r"""
Normalize path.
We normalize ``path`` to:
- Remove the query params.
- Remove any invalid URL chars (\r, \n, \t).
- Always start the path with ``/``.
We don't use ``.path`` to avoid parsing the filename as a full url.
For example if the path is ``http://example.com/my-path``,
``.path`` would return ``my-path``.
"""
parsed_path = urlparse(path)
normalized_path = parsed_path._replace(query="").geturl()
normalized_path = "/" + normalized_path.lstrip("/")
return normalized_path

normalized_path = normalize_path(path)
normalized_full_path = normalize_path(full_path)
normalized_path = self._normalize_path(path)
normalized_full_path = self._normalize_path(full_path)

# add extra fields with the ``path`` and ``full_path`` to perform a
# filter at db level instead with Python.
Expand Down Expand Up @@ -133,3 +113,22 @@ def normalize_path(path):
if new_path:
return new_path, redirect.http_status
return (None, None)

def _normalize_path(self, path):
r"""
Normalize path.
We normalize ``path`` to:
- Remove the query params.
- Remove any invalid URL chars (\r, \n, \t).
- Always start the path with ``/``.
We don't use ``.path`` to avoid parsing the filename as a full url.
For example if the path is ``http://example.com/my-path``,
``.path`` would return ``my-path``.
"""
parsed_path = urlparse(path)
normalized_path = parsed_path._replace(query="").geturl()
normalized_path = "/" + normalized_path.lstrip("/")
return normalized_path

0 comments on commit 0ce44d3

Please sign in to comment.