admin管理员组

文章数量:1291062

I have a link like this:

/?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 instead.

I also have another page like this:

/?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 / and can't be found. How could I do this?

I have a link like this:

https://www.example/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/jobs instead.

I also have another page like this:

https://www.example/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/bh-job/ and can't be found. How could I do this?

Share Improve this question asked Aug 27, 2015 at 8:23 nielsvnielsv 1731 gold badge5 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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).

本文标签: url rewriting404 redirect based on url