plugins

BareProxy Redirects From a File Plugin: Thousands of Old URLs Moved Without Config Lines

Status: built, number 4 of 21 in BareProxy’s build order, and out in BareProxy 0.6.0 as redirects.wasm on the releases page. Its manual is the README in the code repository.

Every site migration leaves a trail of old URLs: a CMS that put dates in paths, a blog that moved from /p/123 to /posts/title, a shop that renamed every category. Each old URL that still has links pointing at it needs a permanent redirect, or the links break and the search ranking that came with them is lost. A migration can easily mean ten thousand redirects.

BareProxy’s core can redirect, one rule per line in the config. That is right for a handful. For ten thousand it isn’t: the config becomes unreadable, and every request would be checked against a long list of rules.

What It Does

  • Reads a plain file. One redirect per line, old path and new address, in a folder the plugin may read and nowhere else. The kind of file a migration script or a spreadsheet export produces.
  • Looks up, doesn’t scan. The file is loaded into a hash table, so a request costs a few lookups whether there are ten redirects or a hundred thousand. A list of 100,000 lines loads in under half a second.
  • Exact paths and prefixes. /p/123 matches itself and /p/123/. /shop/old-cat/* moves a whole section, with the rest of the path carried over, and the longest prefix wins.
  • Keeps the query. The query string goes along to the new address, unless the line says drop-query.
  • Reloads on change. A changed file is picked up on its own, checked first, and a file with a broken line is refused while the old one keeps working. That’s BareProxy’s rule for configs, applied to the redirect list. At the start, check catches the broken line before anything goes live.
  • Counts hits. Each line’s use is counted. A small report page lists them, most used first, and ?unused lists the lines nobody has asked for, the ones that can probably go.

Here is a config:

plugin moved /etc/bareproxy/plugins/redirects.wasm
  config /etc/bareproxy/plugins/moved.conf
  read /etc/bareproxy/redirects

site example.com
  use moved
  route /* -> files /var/www/example/public

The plugin’s own config is a few lines:

# moved.conf
file redirects.txt
report /.redirects

And the list:

# old path              new address
/p/123                  /posts/hello
/about-us               /about/                       308
/search                 /find                         drop-query
/shop/old-cat/*         /shop/new-cat/*
/blog/*                 https://blog.example.com/*    302

Seeing Where Things Went

Every redirected request carries a note in its record with the line of the file that matched, such as “redirects.txt line 12: /p/123 to /posts/hello (301)”. So why on any old URL says exactly where it went and why, and the trace log keeps that for good.

The whole program is on the plugins page.