I could use some help with something I try to achieve. We have a WordPress custom post type with vacancies and all those vacancies have a unique ID like 'P563328-18'. The URL of a single vacancie is like this:
https://example.com/vacancies/vacancie-title-p563328-18/
The vacancie ID's are stored in a ACF (custom field) 'vacancie_id' and I got a code like this to get all vacancie ID's:
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'vacancies',
'meta_key' => 'vacancie_id'
));
if($posts)
{
foreach($posts as $post)
{
echo the_field('vacancie_id');
}
}
?>
I know this is kind of useless for what I want to achieve but then you have an idea.
I want to create an automatic 301 redirect when somebody tries to visit this URL: https://example.com/redirect/P563328-18
The server is redirecting this URL to the vacancie with that unique ID like: https://example.com/vacancies/vacancie-title-p563328-18/
Is something like this possible and can somebody point me in the right direction on how I can achieve this?
Thanks!
EDIT
The answer of Tom made me think and the first URL that needs to get the redirect is a 404.
So basically what I want to achieve that when the 404 has a url like this: https://example.com/redirect/P563328-18
redirect it to a vacancie post that has a slug that matches P563328-18. https://example.com/vacancies/vacancie-title-p563328-18/
Is your first URL valid or generates it a 404?
I`m not really a fan of plugins, but this one works quite good on some of my pages to serve this cause:
https://wordpress.org/plugins/wp-404-auto-redirect-to-similar-post/
Just try it out, it finds a similar post and generates a 301 redirection to that post automatically.
If your first url does not generate a 404 then you could deny access with a.htacess file in the /redirect/ directory, like:
Deny from all
ErrorDocument 404 "/errors/404.html"
Regards Tom
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.