Following this answer, I recently changed the permalink structure of a custom post type called 'Literature' to include the taxonomy term:
Old structure: mysite.com/literature/books/post_name
New structure: mysite.com/literature/fiction/post_name
However, when visiting the original permalink, Wordpress doesn't automatically redirect it and both pages are appearing as separate entries in Google search.
Should I redirect the old literature permalinks? If so, what's the best way to do this?
UPDATE
Thanks Brian, I've amended your code as below and this is working as desired:
add_action('parse_request', 'redirect_books_to_fiction', 0);
function redirect_books_to_fiction(){
global $wp;
if(preg_match('/literature\/books/', $wp->request)){
$redirect = get_bloginfo('siteurl').'/'.str_replace('books', get_the_term_list( $ID, 'literature_category', '', ' / ', '' ), $wp->request);
wp_redirect($redirect, 301);
exit;
}
}
However, how can I redirect everything and not just 'books'?
On my literature permalinks I can type any word into the trail and it doesn't redirect.
e.g. mysite.com/literature/any_random_word/post_name
Just stays as that permalink.
Whereas, if I visit other pages on my site and change any of the trail leading up to a post, it automatically redirects to the correct permalink.
e.g. If I change: mysite.com/food/recipes/post_name
to: mysite.com/a_random_word/another_random_word/post_name
It redirects to the correct permalink.
I assume this is because of the custom permalink structure I used in this answer