I am currently working on a site that has a number of legacy shortlink formats. One of them is particularly problematic because it links to HTML anchors that no longer exist (specifically paragraph numbers using id
without leading zeros). The new format has paragraph numbers with leading zeros; whereas this makes things predictable, it also breaks old shortlinks.
Example
Old Shortlink: https://example.org/?AC_I_1
Intended Target: https://example.com/augsburg-confession/article-i/#ac-i-0001
Redirecting from the old format to the new is relatively trivial, but regex will take one only so far. In this case, the issue lies in the fact that anchor values are not sent to the server, and so cannot be rewritten/redirected. Instead, a client-side solution (e.g., JavaScript) is clearly needed.
Goal
The goal, then, is to have all paragraph numbers (i.e., HTML id
's) with fewer than four digits redirected to their four-digit equivalents (e.g., /#1
>> #0001
).
In searching for a way to accomplish this, I came across this answer that seems like a good start, but I'm uncertain how to alter it to account for the paragraph numbers.