1

I have a link like this:

https://www.example.com/bh-job/38/?title=digital-marketing-intelligence-expert

When I surf to the page I'm redirected 404 page because the page doesn't exisit. The page doesn't exist but I would like to redirect to https://www.example.com/jobs instead.

I also have another page like this:

https://www.example.com/bh-job/121/?title=sem-manager

And this is an actual page and can be found.

So I would need a check where I redirect from the 404 page to /jobs page WHEN the link start with https://www.example.com/bh-job/ and can't be found. How could I do this?

1

I would use the wp hook, which fires right after the request has been parsed and queried:

function wpse_199869_wp( $wp ) {
    if ( ! is_admin() && is_404() && preg_match( '/^bh-job/', $wp->request ) ) {
        wp_redirect( home_url( user_trailingslashit( 'jobs' ) ) );
        exit;
    }
}

add_action( 'wp', 'wpse_199869_wp' );

We make sure it's a 404, and check if the request (URI path) begins with bh-job - if so, redirect to /jobs (the user_trailingslashit function will append or remove a trailing slash to match your permalink structure).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.