I have a custom post type with a rewrite rule containing a variable, which is later replaced by the a category term 'smoothie_category'. The argument line passed to
'rewrite' => ['slug' => 'smoothies/%smoothie_category%'],
An example URL:
I am using the
post_type_link hook to replace this with the post's category term - everything is working correctly thus far.
The issue I have is that the URL can be manipulated to use any string in place of the real category:
Whilst it reaches the correct content, I want to be able to 301 redirect to the correct permalink which I believe is what blog posts do - I have the post category as part of the URL:
/blog/category/post-slug, and using an invalid category slug causes a 301 to the correct post permalink, which is the behaviour I'd like to replicate.
Whilst I can add code in my template, what is the correct, or suggested hook to use to intercept invalid URLs and redirect them to my custom post permalink?