Automatically redirect folders in Sphinx websites#

I spent a bit of time today updating my website after some changes in the MyST-NB and Sphinx Design ecosystems. Along the way, I decided to redirect /posts/ to /blog/, since it seems /blog/ is a much more common folder to use for blog posts.

This posed a problem, because the sphinx-rediraffe extension does not allow you to redirect folders with wildcards. AKA, you cannot do:

rediraffe_redirects = {
    "posts/**/*.md": "blog/**/*.md",

I also didn’t want to have to manually specify every single blog post, since that’d be a very long list.

Fortunately, I figured out a solution because Sphinx’s configuration is also a Python script. This means you can dynamically populate this configuration with pathlib. I’ll share the code snippet below in case it’s useful for others:
# These are posts I *want* to manually specify
rediraffe_redirects = {
    "": "blog/2018/",
# Update the posts/* section of the rediraffe redirects to find all files
redirect_folders = {
    "posts": "blog",
from pathlib import Path
for old, new in redirect_folders.items():
    for newpath in Path(new).rglob("**/*"):
        if newpath.suffix in [".ipynb", ".md"]:
            oldpath = str(newpath).replace("blog/", "posts/", 1)
            rediraffe_redirects[oldpath] = str(newpath)