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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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


1641 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?