How to 301 redirect from one url to another properly

Generally, you can use .htaccess (a file located at the root of the folder you are serving) and a regular expression to do the redirect. I used Yoast to generate mine:

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(?!page/)(.+)$ https://example.com/$4

In the regular expression above I match anything at the beginning then I match / followed by 4 digits (a year) followed by / followed by 2 digits (a month) followed by / followed by 2 digits (a day) followed by the page slug. $4 points to the 4th variable in (…). See here for explanations.

In your example, though, you need to retrieve the date information given the post identifier. In this case, you may find it easier to use a WordPress plugin that will handle the redirection for you. This one (top result in Google) should do the trick.

PS: SEO best practices recommend moving away from date-based URLs. I am in the process of doing just that on my own sites. What’s your reasoning for going the other way?