There are lots of solutions to redirect from the old post URL to the new post URL.
- You can install a plugin “Safe Redirect Manager” and set it from the admin panel.
- You can write some snippet in the
functions.php
file as:
add_action('template_redirect', 'post_redirect_by_custom_filters');
function post_redirect_by_custom_filters() {
global $post;
// this array can contain category names, slugs or even IDs.
$catArray = ['blog'];
if (is_single($post->ID) && !has_category($catArray, $post)) {
$new_url = "http://www.newurl.com/blog/{$post->post_name}/";
wp_redirect($new_url, 301);
exit;
}
}
For more details please follow the below link.
https://www.ryadel.com/en/wordpress-redirect-posts-categories-tags-custom-conditions-301-302/amp/