404 & redirects

Moving directories, renaming slugs, or handling typos calls for two pieces: 404.html at the publish root and redirects from legacy routes to their current destinations.

404 page

Most static hosts (GitHub Pages, Cloudflare Pages, Netlify, Nginx) serve /404.html for unmatched paths. Emit it explicitly in site.typ:

typst
#import "@tola/address:0.0.0": output-url

#document("404.html", format: "html", title: "Page not found")[
  #html.elem("html", attrs: (lang: "en", "data-pagefind-ignore": "all"))[
    #html.head[
      #html.meta(charset: "utf-8")
      #html.meta(name: "viewport", content: "width=device-width, initial-scale=1")
      #html.meta(name: "robots", content: "noindex")
      #html.title[Page not found]
    ]
    #html.body[
      #html.main[
        #html.h1[Page not found]
        #html.p[This page does not exist or has moved.]
        #html.p[#html.a(href: output-url("index.html"))[Back home]]
      ]
    ]
  ]
]

Generating 404.html outside pages keeps it out of sidebars, neighbor links, RSS feeds, and sitemap.xml; adding robots: noindex and data-pagefind-ignore="all" excludes it from search engines and client search indexes.

When deploying under a non-root site.base-path, ensure stylesheets, scripts, and the home link on 404.html use asset-url(...) and output-url(...) instead of relative ./styles.css paths so 404 responses on deep paths like /a/b/c load assets properly.

Static redirects

Record moved routes in page metadata under aliases:

typst
#tola-meta((
  title: "Layouts",
  aliases: ("/docs/guide/metadata-and-navigation/",),
))

Emit a small HTML redirect stub for each alias in site.typ:

typst
#import "@tola/address:0.0.0": output-of, output-url
#import "@tola/site:0.0.0": site

#let redirect-page(from-route, target-output) = {
  let target-url = output-url(target-output)
  let canonical-url = if site.origin != none {
    output-url(target-output, origin: site.origin)
  } else {
    target-url
  }

  document(output-of(from-route), format: "html", title: "Moved")[
    #html.elem("html", attrs: (lang: "en", "data-pagefind-ignore": "all"))[
      #html.head[
        #html.meta(charset: "utf-8")
        #html.meta(name: "robots", content: "noindex")
        #html.link(rel: "canonical", href: canonical-url)
        #html.elem("meta", attrs: ("http-equiv": "refresh", content: "0; url=" + target-url))
        #html.title[Moved]
      ]
      #html.body[
        #html.p[
          Redirecting to #html.a(href: target-url, target-url).
        ]
      ]
    ]
  ]
}

#for page in pages {
  for old-route in page.meta.at("aliases", default: ()) {
    redirect-page(old-route, page.output)
  }
}

Static stubs work everywhere—including GitHub Pages and tola preview—and because each uses document(output-of(old-route), ...), Tola catches any collision with an existing page at build time.

Host redirects

On platforms that support HTTP 301 redirect manifests (Cloudflare Pages, Netlify), emit _redirects with asset(...) from the same aliases metadata instead of HTML stubs:

typst
#import "@tola/address:0.0.0": output-url, route-url

#let redirect-lines = ()
#for page in pages {
  for old-route in page.meta.at("aliases", default: ()) {
    redirect-lines.push(route-url(old-route) + " " + output-url(page.output) + " 301")
  }
}

#if redirect-lines.len() > 0 {
  asset("_redirects", redirect-lines.join("\n") + "\n")
}

Search this site

Use the arrow keys to move through results.

Type to search.