The following is one possible implementation. As long as it is not in the core, copy it into your qa-theme.php:
Better output (there is still no button in the q2a forum CKEditor to insert code with indents, damn):
https://jsfiddle.net/4zyf0o6j/
function initialize() {
if($this->template == 'not-found')
{
// remove slash from beginning of string
$uri = ltrim($_SERVER['REQUEST_URI'], '/');
if(is_numeric($uri))
{
$postid = (int)$uri;
$parentpost = qa_db_read_one_assoc(
qa_db_query_sub('
SELECT `parentid`, `type` FROM ^posts
WHERE `postid` = #', $postid),
true);
if(!empty($parentpost['parentid']))
{
if($parentpost['type'] == 'A')
{
// get question title
$qtitle = qa_db_read_one_value(
qa_db_query_sub('
SELECT `title` FROM ^posts
WHERE `postid` = #', $parentpost['parentid']),
true);
$questionurl = qa_q_path($parentpost['parentid'], $qtitle, false, 'A', $postid);
// redirect to question
qa_redirect_raw($questionurl);
}
else if($parentpost['type'] == 'C')
{
// comment parent is Q or A
$grandparent_post = qa_db_read_one_assoc(
qa_db_query_sub('
SELECT `postid`, `parentid`, `type`, `title` FROM ^posts
WHERE `postid` = #', $parentpost['parentid']),
true);
if($grandparent_post['type']=='Q')
{
$questionurl = qa_q_path($grandparent_post['postid'], $grandparent_post['title'], false, 'Q', $postid);
// redirect to question
qa_redirect_raw($questionurl);
}
else if($grandparent_post['type']=='A')
{
// parent of answer is question, get question title
$qtitle = qa_db_read_one_value(
qa_db_query_sub('
SELECT `title` FROM ^posts
WHERE `postid` = #', $grandparent_post['parentid']),
true);
$questionurl = qa_q_path($grandparent_post['parentid'], $qtitle, false, 'C', $postid);
// redirect to question
qa_redirect_raw($questionurl);
}
}
}
}
} // END if($this->template == 'not-found')
} // END initialize()