In this case script will look like:
<?php
define('FORUM_ROOT', './');
require FORUM_ROOT.'include/common.php';
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($id < 1)
message($lang_common['Bad request']);
// Fetch some info about the topic
if (preg_match('~.*viewtopic.*~', $_SERVER['REQUEST_URI']))
{
$query = array(
'SELECT' => 't.subject AS subject',
'FROM' => 'topics AS t',
'JOINS' => array(
array(
'INNER JOIN' => 'forums AS f',
'ON' => 'f.id=t.forum_id'
),
array(
'LEFT JOIN' => 'forum_perms AS fp',
'ON' => '(fp.forum_id=f.id AND fp.group_id='.$forum_user['g_id'].')'
)
),
'WHERE' => '(fp.read_forum IS NULL OR fp.read_forum=1) AND t.id='.$id.' AND t.moved_to IS NULL'
);
$page = 'viewtopic';
}
else
{
$query = array(
'SELECT' => 'f.forum_name AS subject',
'FROM' => 'forums AS f',
'JOINS' => array(
array(
'LEFT JOIN' => 'forum_perms AS fp',
'ON' => '(fp.forum_id=f.id AND fp.group_id='.$forum_user['g_id'].')'
)
),
'WHERE' => '(fp.read_forum IS NULL OR fp.read_forum=1) AND f.id='.$id
);
$page = 'viewforum';
}
$result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
if (!$forum_db->num_rows($result))
message($lang_common['Bad request']);
list($subject) = $forum_db->fetch_row($result);
if ($page == 'viewtopic')
$redirtect_url = str_replace('&', '&', forum_link($forum_url['topic'], array($id, sef_friendly($subject))));
else if ($page == 'viewforum')
$redirtect_url = str_replace('&', '&', forum_link($forum_url['forum'], array($id, sef_friendly($subject))));
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$redirtect_url);
?>
And the file "<FORUM_ROOT>/.htaccess" will be:
# BEGIN PunBB
<IfModule mod_rewrite.c>
# MultiViews interfers with proper rewriting
Options -MultiViews
RewriteEngine On
# Uncomment and properly set the RewriteBase if the rewrite rules are not working properly
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} viewtopic.* [OR]
RewriteCond %{REQUEST_FILENAME} viewforum.*
RewriteRule . redirect_viewtopic.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . rewrite.php [L]
</IfModule>
# END PunBB