Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite one way then change to another problem

redirect one rewrite method to another?

         

theblade24

12:22 pm on May 3, 2006 (gmt 0)

10+ Year Member



I have a dynamic ecommerce site that for years has had each page rewritten to be SE friendly.

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?

RedAndy

2:19 am on May 10, 2006 (gmt 0)

10+ Year Member



Hi,

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

jdMorgan

2:02 pm on May 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This recent post [webmasterworld.com] describes how to handle the old dynamic URLs, after you have replaced them with static URLs. There *is* a trick to it. See the server variable %{THE_REQUEST} for a way to avoid an infinite dynamic->static->dynamic rewrite/redirection loop.

Jim

RedAndy

6:05 am on May 11, 2006 (gmt 0)

10+ Year Member



I think I need to clarify my post above, having re-read it it isn't clear at all, as was pointed out to me in a sticky

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 :)

RedAndy

5:10 am on May 13, 2006 (gmt 0)

10+ Year Member



Here's the code that I use, not terribly pretty, bugger all error handling and much more suited to the php forum, but it seems relevant.

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