Forum Moderators: coopster
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /myredirect.php?url=$1 [NC,L]
<?php
$url= false;
$topic_id= false;
$forum_id= false;
$start= false;
if (isset($_GET['url']))
{
preg_match('/(about|forum-)([0-9]{1,5})((?:-)([0-9]{1,4}))?.html/', $_GET['url'], $matches);
//see if we matched 'about' or 'forum-'
if (isset($matches[1]) && $matches[1] == 'forum-' || $matches[1] == 'about')
{
//Check for the start parameter which could be present in the topics or forums
if (isset($matches[4]))
{
$start = (int)$matches[4];
}
//If it's forum URL we only need to generate a link for output because any forums we want redirected have already been redirected
if ($matches[1] == 'forum-')
{
$forum_id = (int)$matches[2];
$url = 'https://newdoamin.com/forum/viewforum.php?f=' . $forum_id . ($start ? '&start=' . $start : '');
}
if ($matches[1] == 'about')
{
$topic_id = (int)$matches[2];
//Get the forum ID so we know which forum the topic is in
$link = mysqli_connect( 'localhost', 'readonly_mysqluser', 'password', 'db_name');
$sql = "SELECT forum_id FROM phpbb_topics WHERE topic_id = $topic_id";
if ($result = mysqli_query($link, $sql))
{
while ($row=mysqli_fetch_row($result))
{
$forum_id = $row[0];
$url = 'https://newdomain.com/forum/viewtopic.php?f=' . $forum_id . '&t=' . $topic_id . ($start ? '&start=' . $start : '');
}
mysqli_free_result($result);
}
//Only topics in these forums will be redirected
$valid_forums = array('1', '2', '3');
if (in_array($forum_id, $valid_forums))
{
header("Location: $url",TRUE,302);
exit;
}
}
}
//At this point $url should be false or hold the URL for a forum or topic that wiil not be redirected.
header("HTTP/1.0 404 Not Found");
//Switch to 410 after a few days once the logs are checked for anything that was missed
//header("HTTP/1.0 410 Gone");
}
?>
Output some HTML ....If $url is not false
The page you requested in no longer avaiable here but can be found here:
echo $url
-or-
page not found, blah blah blah
if (isset($matches[1]) && $matches[1] == 'forum-' || $matches[1] == 'about') if (isset($matches[1]) && ($matches[1] == 'forum-' || $matches[1] == 'about')) I would probably just stick with the .html rewrite since /viewtopic.php?etc isn't any friendlier
and I would do a 301 redirect,
Also, in phpBB it's optional to pass the forum ID in the URL of a thread, so you could technically do this with .htaccess alone.
I realize that but it's maintenance issue that I'll be glad to be rid of and I'm also upgrading the forum software to latest version
/(about|forum-)([0-9]{1,5})((?:-)([0-9]{1,4}))?.html/ [3] = - plus the rest of the numbers
...If the - element is always present,
(Oh and psst! You really need to escape the literal period in \.html, since a . alone would also match a digit.)
Ugh, yes, that does bring back a few memories of a commercial, closed-source module
I assume you'll also be escaping any HTML characters in $url
The minus sign and the group of number after it will appear together or not all.
...
I do not need to capture it. I only need the numbers that follow it
(about|forum-)([0-9]{1,5})(?:-([0-9]{1,4}))?\.htmlwhere [3] = the second set of numbers, if present. Yes, the group will be captured even if it's inside a (?: blahblah) non-capture envelope. (about|forum-)(\d+)(?:-(\d+))?\.html