0

We had a custom CMS with URLs such as /adele, /slayer, /beastie-boys but now they have the structure /artist/adele, /artist/slayer, /artist/beastie-boys (a custom post type)

Obviously when people visit the original links, they get a 404. Is there a way/what's the best way to check whether whether the post exists in the new format first before throwing out a 404 error? There's 80k or so artists in the database.

We're on IIS so could make use of a rewritemap but it'll probably be too massive

  • .htaccess redirect? – NoBugs Nov 15 '15 at 3:20
  • On IIS? Nope!!! – pee2pee Nov 15 '15 at 20:35
2
function checkArtist() {
    $page = get_page_by_path($_SERVER['REQUEST_URI'].'/',OBJECT,'artist');

    if($page){
        header("HTTP/1.1 301 Moved Permanently"); 
        header("Location: /artist".$_SERVER['REQUEST_URI']);
    }
}
add_action( 'wp', 'checkArtist' );

I add a custom hook using wp which as the article says:

This hook is one effective place to perform any high-level filtering or validation, following queries, but before WordPress does any routing, processing, or handling.

The function then uses get_page_by_path to retrieve a page given its path. The path is took from the URL using PHP $_SERVER['REQUEST_URI']. If it exists for that particular post type then I do a 301 redirect.

  • Please add an explanation of what your code does and how it works. Your answer has already ben flagged as low quality due to the lack of info. Thank you – Pieter Goosen Nov 8 '15 at 8:37
  • I'm sorry for causing so much distress to the community - I will amend as necessary. – pee2pee Nov 8 '15 at 9:14
  • No problem. ;-) Always nice when someone comes back to answer his own question. Just remember to accept it as well as soon as the restriction is lifted – Pieter Goosen Nov 8 '15 at 9:20

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.