Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
2.1k views
in Q2A Core by
edited by
If anybody want to redirect error404 to a search page, then here is a trick for you.

I have recently changed my blog to a Q2A site. But there was a problem : Site was already indexed by gooogle and there are many pages of my site are at the top of google search. So, I change my site to Q2A then, its not possible to keep same link as my previous script. As a result my site ranking and visitors will decrease.

So, I thought, Insted of sending user to a error404 page why not redirect them to a search page.

yeak!!! sounds intersting laugh

I did this:

Simply replace whole code of

qa-include/qa-page-not-found.php

with this one:

    <?php

    /*
        copyright was removed cuz i m not able to post more then 8000 chr
    */

        if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
            header('Location: ../');
            exit;
        }

        require_once QA_INCLUDE_DIR.'qa-app-format.php';
        require_once QA_INCLUDE_DIR.'qa-app-options.php';
        require_once QA_INCLUDE_DIR.'qa-app-search.php';

    //    Perform the search if appropriate

        
        //    Pull in input parameters
            $urlfor    =    strtolower($_SERVER['REQUEST_URI']);
            $urlfrom404 = rtrim($urlfor,"/");
            $tokens = explode('/', $urlfrom404);
            $keys = array_keys($tokens);
            $last = end($keys);
            $removeHyphen = strtr($tokens[$last], '-', ' ');
            
            $inquery=$removeHyphen;

            $userid=qa_get_logged_in_userid();
            $start=qa_get_start();
            
            $display=qa_opt_if_loaded('page_size_search');
            $count=2*(isset($display) ? $display : QA_DB_RETRIEVE_QS_AS)+1;
                // get enough results to be able to give some idea of how many pages of search results there are
            
        //    Perform the search using appropriate module

            $results=qa_get_search_results($inquery, $start, $count, $userid, false, false);

        //    Count and truncate results
            
            $pagesize=qa_opt('page_size_search');
            $gotcount=count($results);
            $results=array_slice($results, 0, $pagesize);
            
        //    Retrieve extra information on users    
            
            $fullquestions=array();
            
            foreach ($results as $result)
                if (isset($result['question']))
                    $fullquestions[]=$result['question'];
                    
            $usershtml=qa_userids_handles_html($fullquestions);
            
        //    Report the search event
            
            qa_report_event('search', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
                'query' => $inquery,
                'start' => $start,
            ));

    //    Prepare content for theme

        $qa_content=qa_content_prepare(true);

            $qa_content['search']['value']=$removeHyphen;
        
            if (count($results))
                $qa_content['title']=qa_lang_html_sub('main/results_for_x', qa_html($inquery));
            else
                $qa_content['title']=qa_lang_html_sub('main/no_results_for_x', qa_html($inquery));
                
            $qa_content['q_list']['form']=array(
                'tags' => 'METHOD="POST" ACTION="'.qa_self_html().'"',

                'hidden' => array(
                    'code' => qa_get_form_security_code('vote'),
                ),
            );
            
            $qa_content['q_list']['qs']=array();
            
            $questionoptions=qa_post_html_defaults('Q');
            
            foreach ($results as $result)
                if (!isset($result['question'])) { // if we have any non-question results, display with less statistics
                    $questionoptions['voteview']=false;
                    $questionoptions['answersview']=false;
                    $questionoptions['viewsview']=false;

                    $fakeoptions=$questionoptions;
                    $fakeoptions['whoview']=false;
                    $fakeoptions['whenview']=false;
                    $fakeoptions['whatview']=false;
                    
                    break;
                }
            
            foreach ($results as $result) {
                if (isset($result['question']))
                    $fields=qa_post_html_fields($result['question'], $userid, qa_cookie_get(), $usershtml, null, $questionoptions);
                
                elseif (isset($result['url']))
                    $fields=array(
                        'what' => qa_html($result['url']),
                        'meta_order' => qa_lang_html('main/meta_order'),
                    );

                else
                    continue; // nothing to show here
                
                $fields['title']=qa_html($result['title']);
                $fields['url']=qa_html($result['url']);
                
                $qa_content['q_list']['qs'][]=$fields;
            }

            $qa_content['page_links']=qa_html_page_links(qa_request(), $start, $pagesize, $start+$gotcount,
                qa_opt('pages_prev_next'), array('q' => $inquery), $gotcount>=$count);
            
            if (qa_opt('feed_for_search'))
                $qa_content['feed']=array(
                    'url' => qa_path_html(qa_feed_request('search/'.$inquery)),
                    'label' => qa_lang_html_sub('main/results_for_x', qa_html($inquery)),
                );

            if (empty($qa_content['page_links']))
                $qa_content['suggest_next']=qa_html_suggest_qs_tags(qa_using_tags());

        

            
        return $qa_content;

    /*
        Omit PHP closing tag to help avoid accidental output
    */

here is demo:

http://tovolt.com/2013/02/best-online-shopping-site/
Q2A version: 1.6
by
Update: Google loop was caused by my server handling URLs with %3F.

1 Answer

+1 vote
by
[05-Mar-2013 10:23:06] PHP Fatal error:  Call to undefined function  qa_get_form_security_code() in /qa-include/qa-page-not-found.php on line 94

What/Where is qa_get_form_security_code() ?
by
Which version you are using?? is it 1.6 ?? my code is for 1.6dev. If you are using 1.6 then I will check it
by
I am using 1.5.4, okay, then I should check the source code of 1.6...
by
Iam using 1.6.2 and getting the same error
Fatal error: Call to undefined function qa_get_form_security_code() in /qa-include/qa-app-q-list.php while using twitter bootstrap theme
...