Redirect Lost Posts In WordPress using the Requested URI

Recently I changed my post URLs to include the category before the post slug. While this is a redirect that WordPress can usually handle, I found that redirects coming from my old URL were not routing correct (they did not include the category). This simple redirect gets the post object using the REQUEST_URI and, if found, will redirect to the correct post.

You can also do this with different post types using the $post_type parameter of get_page_by_path().