Preparing Your New Site for Structural Changes - The Code
(Page 6 of 6 )
This is the code you need to see all this in action. Enjoy!
Database Creation Script
CREATE TABLE relocatedcontent (
URI_ID int(11) NOT NULL auto_increment,
OldURI varchar(100) NOT NULL default '',
NewURI varchar(100) NOT NULL default '',
PRIMARY KEY (URI_ID)
) TYPE=MyISAM CHARSET=latin1;
#
# Dumping data for table 'relocatedcontent'
#
INSERT INTO relocatedcontent VALUES("1", "/OldPage.htm", "NewPage.htm");
.htaccess code
# .htaccess
ErrorDocument 404 /404.php
404 PHP Script
<?
// 404.php
include("db.php");
// Get the name of the file requested into an
// array split on the "?"
$FileParts = explode ( "?", $_SERVER["REQUEST_URI"]);
// Lets just use everything prior to the question
//mark, as the upcoming query will not match if
//there are arguments in the string.
$FileRequested = $FileParts[0];
// Get the server name to generate an absolute
//path for the redirect
$ServerName = $_SERVER["SERVER_NAME"];
// Setup the DB object for our query
$q = new DB;
// Look and see if the requested file is in the
// database
$query = "SELECT * from RelocatedContent WHERE OldURI = '$FileRequested'";
// Run the query
$q->RunQuery($query);
// If we found a match..
if ($q->GetNextRecord()){
$NewURL = $q->Fields("NewURI");
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://$ServerName/$NewURL");
}
// If we made it this far, we have a "real"
// 404 on our hands, show 'em some options..
include("404form.htm");
?>
Database Connection Code
<?
//db.php
class DB {
var $classname = "DB";
var $Host = "localhost";
var $Database = "SystemDatabase";
var $User = "SomeUserName";
var $Password = "SomePassword";
var $result;
var $line;
var $link;
function DB() {
$this->link = mysql_connect($this->Host,$this->User,$this->Password) or die("Could not connect");
mysql_select_db($this->Database) or die("Could not select database");
return $this->link;
}
function RunQuery($val){
$this->result = mysql_query($val) or die(mysql_error());
return $this->result;
}
function GetNextRecord(){
$this->line = mysql_fetch_assoc($this->result);
return $this->line;
}
function Fields($val){
return $this->line[$val];
}
}
?
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |