One option is to completely forget about the old links in your .htaccess file. Instead, create a custom 404 Not Found error page and configure Apache to serve it with broken links. This page will be a PHP script, and it will check the variable $_SERVER['REQUEST_URI'] for a simple expression:
<?php if( substr($_SERVER['REQUEST_URI'], 0, 7)=='old.php' ){
Inside the "found old link" condition, you can perform further processing:
- Run regex to find article id
- Do the math and try to match it with the new ID
- Perform a permanent redirect with the header ()
source share