Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

how dirty are you


Go to End


1443 Views

Avatar
Nicolaas

Forum Moderator, 224 Posts

31 May 2007 at 3:42pm

In terms of dirty hacks, this has to be right up there.....

In the mysite/_config.php folder, I added:

//permanent redirections .....
$url = $_SERVER["REQUEST_URI"];
$url = substr($url, 1, 200);

if($url) {
dbcon("xxx", "xxx", "xxx", "xxx", "error connecting");
$sql = 'select count(*) FROM `SiteTree` WHERE LegacyUrl = "'.$url.'";';

if(mysqlCount($sql)) {
$sql = 'select `LegacyUrl`, `URLSegment` FROM `SiteTree` WHERE LegacyUrl = "'.$url.'";';
$data = getdata($sql);
foreach($data as $ds) {
$newLocation = str_replace("--", "-", str_replace("---", "-", "/".$ds["URLSegment"]."/"));
header("HTTP/1.1 301 Moved Permanently");
header("location: $newLocation");
die();
}
}
}

function dbcon($username, $password, $host, $mydb, $msg) {
// Database-specific information:
$connect = mysql_pconnect ($host, $username, $password) or die ($msg);
mysql_select_db ($mydb, $connect) or die ($msg);
return 1;
}

function getdata($sql) {
//returns a multidimensional array for the sql
$result = @mysql_query($sql) or die("Error: " . mysql_error()." sql was ".$sql);
$ret = array();
while($row = mysql_fetch_assoc($result)) {
$ret[] = $row;
}
mysql_free_result($result);
return $ret;
}

function mysqlCount($sql) {
$result = @mysql_query($sql) or die("Error: " . mysql_error()." sql was ".$sql);
if($result) {
if (mysql_num_rows($result) != 1) {
return false;
}
return mysql_result($result, 0,0);
}
else {
die("error in mysql_zz: ".$sql);
}
}

This gives me a redirect for all the LegacyUrls..... Surely there is a better way????

Any recommendations?