Forum Moderators: phranque
I recently changed the method of the rewrite so as not to include the page title in the url to allow keywords to now be spidered in the url.
I'm trying to eliminate removing the rewrite rules for the old method, hence rendering all those links inoperative.
Is it possible to redirect one method to the other so that already spidered links will redirect to the news one?
I got around this by running the standard script when a request comes in and having it attempt to find the request page in the new set up.
If it fails then before returning a 404 it checks a DB table for a map of the old urls to the new. If it find a record then it sends a 301 with the new url, if not then it send the standard 404. It works well for me because over time there are less and less 'rewrites' to be done as search engines drop the old links and referrers update their links,
hth
Andy
Jim
What I meant was - when a request comes in for an old url, say
/products/widgets/fantastic_green_widgets
the script goes and tries it as if it was a page in the new set up. Naturaly it fails. Before returning a 404 it calls another script which checks a db table with two fields. These are old_page (varchar primary key) and new_page (var_char), if the page exists then it sends a 301 to redirect to the new page, if not then it calls a 404.
I hope that's a better explanation :)
Anytime my CMS wants to send a 404 because the requested page is not in the database it goes and checks the 'map_old_new' table, and sends a 301 if it can instead of a 404.
function senda404($url) {
/* check if we can redirect from an old url */
if (isset($url)) {
$sql="SELECT new FROM map_old_new WHERE old='/".$url."'";
$rst=mysql_query($sql);
if($row=mysql_fetch_array($rst,MYSQL_ASSOC)) {
redirect($row['new']);
die();
}
}
generate_page("/".SC_404);
$msg="URL=$url \n";
$uri=(isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']:"";
$msg.="REQUEST_URI=".$uri."\n";
$ref=(isset($_SERVER['HTTP_REFERER']))?$_SERVER['HTTP_REFERER']:"";
$msg.="HTTP_REFERER=".$uri."\n";
$agent=(isset($_SERVER['HTTP_USER_AGENT']))?$_SERVER['HTTP_USER_AGENT']:"";
$msg.="HTTP_USER_AGENT=".$agent."\n";
$ip=(isset($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR']:"";
$msg.="REMOTE_ADDR=".$ip."\n";
$header="From: Site Error@example.com <TheSite@example.com>\r\n";
mail(EMAIL_ERRORS_ADDRESS,"EX 404 - ". date('d')."/".date('m')."/".date('Y')." ".date('H').":".date('i'), $msg, $header);
}
function redirect($url) {
header("HTTP/1.0 301 Moved Permanently");
header("Location: http://".SERVER_NAME.$url);
}
hth,
Andy