admin管理员组文章数量:1122846
I have a custom post type containing job listings, that is highly volatile. Jobs are frequently added and removed. Our analytics show that a lot of crawling errors are for detail pages of jobs that have been unlisted.
The solution I came up with is to redirect all visits for non-existant URLs within the CPT's slug to the job listing overview, and I would like to automate this.
How would I go about this? I'm looking for a solution that does this very early and skips as much unnecessary calls as possible. (i.e. something earlier than doing this in header.php, ideally as an action)
Example:
mydomain/jobs/existingjob/
delivers the detail page for the jobmydomain/jobs/nojobhere/
does not exist and would throw a 404 but instead gets redirected tomydomain/jobs/
I have a custom post type containing job listings, that is highly volatile. Jobs are frequently added and removed. Our analytics show that a lot of crawling errors are for detail pages of jobs that have been unlisted.
The solution I came up with is to redirect all visits for non-existant URLs within the CPT's slug to the job listing overview, and I would like to automate this.
How would I go about this? I'm looking for a solution that does this very early and skips as much unnecessary calls as possible. (i.e. something earlier than doing this in header.php, ideally as an action)
Example:
mydomain.com/jobs/existingjob/
delivers the detail page for the jobmydomain.com/jobs/nojobhere/
does not exist and would throw a 404 but instead gets redirected tomydomain.com/jobs/
3 Answers
Reset to default 4It looks like template_redirect
is as far up the WordPress action chain you can go while still being able to detect that a 404 error is being thrown. This action will trigger before any template files would be loaded so it will happen before loading unnecessary resources.
You can try adding this into your /wp-content/themes/yourtheme/functions.php
file to achieve a dynamic redirect for all 404's that happen when viewing single jobs:
add_action( 'template_redirect', 'unlisted_jobs_redirect' );
function unlisted_jobs_redirect()
{
// check if is a 404 error, and it's on your jobs custom post type
if( is_404() && is_singular('your-job-custom-post-type') )
{
// then redirect to yourdomain.com/jobs/
wp_redirect( home_url( '/jobs/' ) );
exit();
}
}
I don't have the rep to comment on Ben HartLenn's answer, and I know this is an old post, but to save future users from wasting time: is_singular('post_type') will not work on a 404 page. $wp_query->queried_object will also not work.
This does work:
add_action( 'template_redirect', 'unlisted_jobs_redirect' );
function unlisted_jobs_redirect(){
//check for 404
if( is_404()){
global $wp_query;
//check that wp has figured out post_type from the request
//and it's the type you're looking for
if( isset($wp_query->query['post_type']) && $wp_query->query['post_type'] == 'your-job-custom-post-type' ){
// then redirect to yourdomain.com/jobs/
wp_redirect( home_url( '/jobs/' ) );
exit();
}
}
If there is no page on your site with that post type, you need to ask yourself the question: should I really be deleting that page? Or should I just make it better? If you decide to make sure you send the proper HTTP header: a 410 content deleted header.
404 and 410 HTTP headers The difference between a 404 and a 410 header is simple: 404 means “content not found”, 410 means “content deleted” and is thus more specific. If a URL returns a 410, Google is far more certain you removed the URL on purpose and it should thus remove that URL from its index. This means it will do so much quicker.
You can use Yoast SEO Premium plugin, the redirects module in this plugin is capable of serving 410 headers.
本文标签: phpHow do I redirect all 404 errors of a specific post type to another URL
版权声明:本文标题:php - How do I redirect all 404 errors of a specific post type to another URL? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305786a1932712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论