Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
466 views
in Q2A Core by

I need to redirect deleted questions to main question page. This is because the deleted questions keep with the URL working.

See: https://novonegocio.com.br/respostas/procuro-trabalho-em-casa

I tried adding a 301 htaccess redirect, like bellow:

Redirect 301 "/respostas/1002/lavar-alugar-toalhas-esterilizadas-abrir-firma-fiscal-boleto" "/respostas/"

But it redirect to other url with the "qa-rewrite=" in the midle, see...

"https://novonegocio.com.br/respostas/?qa-rewrite=1002/lavar-alugar-toalhas-esterilizadas-abrir-firma-fiscal-boleto"

This 301 redirect is very important to SEO.

May someone help me?

Thank you in advance.

1 Answer

+1 vote
by
selected by
 
Best answer

You'll have to write a custom plugin with a process module. In the process module add the the following function.

1. Create the file qa-plugin/redirect-plugin/qa-plugin.php with this content:

<?php

if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
    header('Location: ../../');
    exit;
}
qa_register_plugin_module('process', 'RedirectDeletedProcess.php', 'RedirectDeletedProcess', 'Redirect Deleted Process');

2. Create the file qa-plugin/redirect-plugin/RedirectDeletedProcess.php with this content:

<?php

class RedirectDeletedProcess {
    public function plugins_loaded()     {
        require_once QA_INCLUDE_DIR . 'app/page.php';

        $requestlower = strtolower(qa_request());
        $requestparts = qa_request_parts();
        $firstlower = strtolower($requestparts[0]);
        $routing = qa_page_routing();

        if (isset($routing[$requestlower])) {
            return;
        }

        if ($firstlower === 'user' && isset($requestparts[1])) {
            $account = qa_db_single_select(qa_db_user_account_selectspec($requestparts[1], false));
            if (!is_array($account)) {
                $this->redirectTo('users');
            }
        }

        if (!isset($routing[$firstlower . '/']) && is_numeric($requestparts[0])) {
            $question = qa_db_single_select(qa_db_full_post_selectspec(qa_get_logged_in_userid(), $requestparts[0]));
            if (!isset($question)) {
                $this->redirectTo('questions');
            }
        }
    }

    private function redirectTo($location)     {
        header("HTTP/1.1 301 Moved Permanently");
        qa_redirect($location);
    }
}

by
Hi!

Thank you, but I'm not a php expert. :-/

Do you know someone that could do this job?

And, if yes, how much the work?

Thank you in advance!
by
I've just updated the answer. All you need to know is to create files and copy/paste text into it :)
by
It's working perfectly!

You are the guy! :-)

Do you know it the plugin is doing a 301 redirect?

May you send me, by private message, your paypal e-mail?
by
It is doing a 301 redirect. You can see that yourself by pressing F12 in your browser, go to the Network tab and try to access an invalid question ID. Check this image: https://imgur.com/a/uo6KvFl
by
Very nice!

Can we include the deleted users here too? So the URLS would be redirect to /questions or /users.

This should clean any problem with duplicated content indexed by Google.
by
Updated my answer. If you need anything else, maybe it'd be better for it to be part of a different question
by
It worked perfectly! Thank you!

May you send me, by private message, your paypal e-mail?

Thanks!
by
Thanks! You can use this PayPal link: https://www.paypal.me/pupi1985 :)
by
Thanks Gabriel!
...