I have implemented a solution similar to what you are asking for.
There are two parts to my solution.
Known redirects are handled by the server in the web.config file. By maintaining the web.config file with known redirects, the server will automatically redirect to the "correct" page. This requires an IIS server, but other server technologies should have similar functions (see '.htaccess Syntax').
web.config Syntax
<rewriteMaps>
<rewriteMap name="redirects">
<add key="/123" value="/new-location" />
</rewriteMap>
</rewriteMaps>
.htaccess Syntax
Redirect /index.html http://example.com/newdirectory/
The other part of the solution is to create a custom 404 page that can access your database and manually redirect the user.
PHP / mySQL
<?php
$db = mysqli_connect('YOURHOST','YOURDATABASE','YOURDATBASEPASSWORD','YOURDATABASEUSER');
$requestedURL = $_SERVER['REQUEST_URI'];
$sql = "SELECT redirectURL FROM tblurls WHERE requestURL LIKE '%" . $requestedURL . "%'";
$result = mysqli_query($db,$sql);
if(mysqli_num_rows($result) == 0) {
// no matches
}
else {
$row = $result->fetch_assoc();
echo "<a href='" . $row['redirectURL'] . "'>Click to go here.</a>";
};
?>
In IIS I have setup a custom 404 system using mySQL and PHP. Depending on your flavor of server and programming, you will want to get the requested url, check that value against your database and finally redirect to the associated value.
JavaScript Redirect
<script type='text/javascript'>
window.location.href = '/newlocation/';
</script>
KEEP IN MIND - If you are concerned about search engine optimization, you may want to provide a link to the correct page rather than automatically directing the user. Search engines don't like automatic redirects.
Hope that helps.