Using a custom field value to redirect away from a 404 page

I can’t say I would recommend this on a highload system, if anything these should be 301’d in HTAccess or have their own custom table to relate to. Below is a solution which will grab the requested URL and try to find post with matching meta:

/**
 * Redirect 404s based on requested url
 *
 * @return void
 */
function wpse_226378() {
    global $wp;

    if( is_404() ) {
        $current_url = add_query_arg( $_REQUEST,$wp->request );     // Get current requested URL ( minus the domain )
        $page = get_posts( array(                                   // Get a single post that has that URL as a post meta
            'post_type'         => 'page',
            'posts_page_page'   => 1,
            'meta_key'          => 'old_url',
            'meta_value'        => sanitize_text_field( $current_url ),
            'fields'            => 'ids',
        ) );

        if( ! empty( $page ) ) {                                    // IF it's found
            wp_safe_redirect( get_permalink( $page[0] ), 301 );     // 301 redirect it
            exit;                                                   // Exit script entirely
        }
    }
}
add_action( 'template_redirect', 'wpse_226378' );