custom 404 error page
{source}
<?php
#
#This is the custom error page for HTML error 404 – “file not found”
#
#echo “request to {$_SERVER[‘REQUEST_URI’]}” ;
##echo “<pre>”;#print_r($_SERVER); #echo “</pre>”;
preg_match_all(“#/.*?&url=/(.*?)(d*?-.*?)&qstr.*#is”, $_SERVER[‘QUERY_STRING’]);
##echo “<p>”.$_SERVER[‘QUERY_STRING’].”</p>” ;
##echo “<pre>”; #print_r($matches); #echo “</pre>”;
##echo “basename = “.basename($_SERVER[‘QUERY_STRING’]). “<br>”;
parse_str($_SERVER[‘QUERY_STRING’], $output);
##echo “<pre>”;#print_r($output);#echo “</pre>”;
##echo “basename url = “.basename($output[url]); #echo “<br>”;
preg_match(“/(d*?)-(.*)/is”, basename($output[url]), $matches );
$the_old_id = $matches[1];
$the_alias = $matches[2];
$the_url = $output[url];
#echo “<br>get the article id for $the_alias <br>” ;
$temp = explode( “/” , $output[url] );
#echo “<pre>$temp = “; #print_r($temp); #echo “</pre>”;
##echo “the article alias = <pre>$matches”; #print_r($matches); #echo “</pre>”;
$now = time();
$query = “SELECT a.catid, a.id, a.state, a.alias as alias, a.title, a.publish_up, a.publish_down, a.created
FROM #__content AS a
WHERE alias = ‘$the_alias’
AND a.state = 1″;
$query .= ” AND ( a.publish_down = ‘0000-00-00 00:00:00’ OR a.publish_down >= $now )”;
$query .= ” ORDER BY a.ordering ; ” ;
echo “n!– query = $query –>n”;
$db->setQuery( $query );
$this_item = $db->loadObjectList();
$count = count($this_item);
#echo “<!– query – $count items found –>”;
if ($count == 0 ) {
header (“location: /article-cannot-be-found?count=$count&the_url=$the_url”);
exit;
}
echo “!– first set <br /><pre>”;#print_r($this_item) ; #echo “–></pre>”;
$the_id = $this_item[0]->id ;
echo “<br>$the_alias = $the_alias, $the_url = $the_url, $the_old_id = $the_old_id ,$the_id = $the_id “;
$new_url = str_replace(“$the_old_id”, “$the_id”, $the_url);
#echo “<br> the new url = $new_url <br>”;
header( “Location: $new_url”, TRUE, 302 );
exit;
?>
{/source}