$TVname = 'oldURI';
$current = $_SERVER["REQUEST_URI"];
$table = $modx->getFullTableName("site_tmplvars");
$TVid = mysql_fetch_object($modx->db->select('id', $table, "name = '$TVname'"))->id;
$table = $modx->getFullTableName("site_tmplvar_contentvalues");
$doc = mysql_fetch_object($modx->db->select('contentid', $table, "value='$current' and tmplvarid='$TVid'"))->contentid;
$url=$modx->makeUrl($doc);
$modx->sendRedirect($url, 1);
$TVname = 'oldURI';
$current = $_SERVER["REQUEST_URI"];
$table = $modx->getFullTableName("site_tmplvars");
$TVquery = $modx->db->select('id', $table, "name = '$TVname'","","1");
$TVrow = $modx->getRow($TVquery);
$TVid = $TVrow["id"];
$table = $modx->getFullTableName("site_tmplvar_contentvalues");
$docQuery = $modx->db->select('contentid', $table, "value='$current' and tmplvarid='$TVid'");
$docID = $modx->getRow($docQuery);
$url = $modx->makeUrl($docID);
$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', '301');
$TVname = 'oldURI';
$current = $_SERVER["REQUEST_URI"];
$tmplvars = $modx->getFullTableName("site_tmplvars");
$contentvalues = $modx->getFullTableName("site_tmplvar_contentvalues");
$docQuery = $modx->db->query("SELECT contentid FROM $tmplvars INNER JOIN $contentvalues ON $tmplvars.id = $contentvalues.tmplvarid WHERE $tmplvars.name = '$TVname' AND $contentvalues.value = '$current'");
$docRow = $modx->db->getRow($docQuery);
$url = $modx->makeUrl($docRow['contentid']);
$modx->sendRedirect($url, 1, 'REDIRECT_HEADER', '301');
$modx->sendRedirect($url, 1, '', 'HTTP/1.0 301 Moved Permanently');
$modx->sendRedirect($url, 1, '', 'HTTP/1.1 301 Moved Permanently');
$TVname = 'oldURI';
$e = &$modx->Event;
switch ($e->name) {
case "OnWebPagePrerender":
$to_redirect = $modx->config['to_redirect'];
if ($to_redirect > 0) {
$modx->config['to_redirect'] = null;
$url = $modx->makeUrl($to_redirect);
$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', '301');
}
break;
case "OnPageNotFound":
$current = $_SERVER["REQUEST_URI"];
$tmplvars = $modx->getFullTableName("site_tmplvars");
$contentvalues = $modx->getFullTableName("site_tmplvar_contentvalues");
$docQuery = $modx->db->query("SELECT contentid FROM $tmplvars INNER JOIN $contentvalues ON $tmplvars.id = $contentvalues.tmplvarid WHERE $tmplvars.name = '$TVname' AND $contentvalues.value = '$current'");
$docRow = $modx->db->getRow($docQuery);
$modx->config['to_redirect'] = $docRow['contentid'];
break;
default:
return; // stop here - this is very important.
break;
}
You could write a plugin that OnPageNotFound takes...
$TVname = 'oldURI';
if ($modx->Event == "OnWebPageInit")
{
if (!$modx->documentIdentifier) // Check for 404
{
$current = $_SERVER["REQUEST_URI"];
$tmplvars = $modx->getFullTableName("site_tmplvars");
$contentvalues = $modx->getFullTableName("site_tmplvar_contentvalues");
$docQuery = $modx->db->query("SELECT contentid FROM $tmplvars INNER JOIN $contentvalues ON $tmplvars.id = $contentvalues.tmplvarid WHERE $tmplvars.name = '$TVname' AND $contentvalues.value = '$current'");
if ($modx->getRecordCount($docQuery) > 0)
{
$id = $modx->getValue($docQuery);
$url = $modx->makeUrl($id);
$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', '301');
}
}
}
return;
Cool plugin, I’ve been looking to so something very similar. Opengeek’s solution would work but will end up cluttering the document tree, especially when you are dealing with many page redirects.
Prerender may be a little late to send the redirect, although it should work fine. Instead of chaining the events and storing data as a configuration parameter, I would use the OnWebPageInit event:
This discussion is closed to further replies. Keep calm and carry on.