Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » how dirty are you

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

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

Page: 1
Go to End
Author Topic: 1205 Views
  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    how dirty are you Link to this post

    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?

    1205 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.