Turning Broken URLs Into Search Terms?

I’m migrating a Typepad install to a WordPress subdomain, and most of the links will be broken. The rewrites work so that:

http://mainsite.com/blogs

is rewritten to

Read More
http://blogs.mainsite.com

If a full link with article information is passed, it is rewritten as such;

http://mainsite.com/blogs/2014-13-7/article-title.html

to

http://blogs.mainsite.com/2014-13-7/article-title.html

I am looking for a way to feed the article title into WordPress’s search so even though the link is broken, it is easy for users with bookmarks find it.

Related posts

Leave a Reply

2 comments

  1. Why not do a one-to-one redirect? When migrating the data over, add a custom field like ‘_legacy_url’ in the WXR or whatever mechanism you’re using to track the old URLs. Then, check during the template_redirect action for a one-to-one match whenever WP throws a 404.

    add_action('template_redirect', 'typepad_redirects', 0);
    function typepad_redirects(){
        if(is_404()){
            global $wpdb, $wp;
            $url = esc_attr($wp->request);
            $result = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_legacy_url' AND meta_value LIKE '%$url%'");
            $redirect_url = get_permalink($result->post_id);
            if($redirect_url){
                 wp_redirect($redirect_url, 301);
                 die();
            }
        }
    }