Redirecting Pages

before we can MovePages we'll need a redirector to keep the links to the old wikiword intact. this facility is useful for other reasons too. it can serve synonyms, acronyms and different transcriptions of foreign names. we'll hook in at the function run() after we've got all information we need (the page itself and it's acls):

run():

<?
    // ...
    if ($this->method == "show" && $_REQUEST["redirect"] != "no") $this->GetTargetPage();
    $this->ReadInterWikiConfig();
?>


if we want to call the function gettargetpage() we'd better have one:

<?
function GetTargetPage() {
    if ($this->HasAccess("read")) {
        if (preg_match("/\A\s*=>\s*(.*)/", $this->page["body"], $matches)) {
            // patterns of forced links and wikiwords (without the commas) are german umlauts really enough?
            preg_match ("/^(\[\[.*?\]\]|[A-ZÄÖÜ][a-zäöüß]+[A-Z0-9ÄÖÜ][A-Z0-9a-zäöüßÄÖÜ]*)/", $matches[1], $matches1);
            $target = $matches1[1];
                if ($target_page = $this->LoadPage($target)) {
                $this->redirect_page = $this->page["tag"];
                $this->LoadAllACLs($target_page["tag"]); // isn't loadallacls better a part of setpage()?
                $this->SetPage($target_page);
            }
        }
    } else {
        // no idea why a redirector page should have it's own acls. but if user has no read-access he hasn't ;)
        // in that case, we simply do nothing and let the page fail as usual
        return;
    }
}
?>



the showhandler shall provide a link back to the original page. or we'll never have a chance to change the redirector (of course we would, guess how).

handlers/page/show.php

<?
    // ...

    if ($this->redirect_page) {
        print ("<div class='revisioninfo'>Redirected from "); // this div may get it's own class
        print ("<a href='".$this->href("", $this->redirect_page, "redirect=no")."'>".$this->redirect_page."</a>");
        print ("</div>\n");
    }

    // display page
    print($this->Format($this->page["body"]));
?>


Test,Page

now any page will be redirected to another when it starts with =>TargetPage


CategoryDevelopmentHandlers CategoryDevelopmentCore
Comments
Comment by MyTreo
2006-05-20 11:02:04
Any way to make this include a HTTP 301 header (permanent redirect) when the old page is called? This would help a great deal with spider indexing to prevent it looking like there is repeated content.
Comment by MyTreo
2006-05-20 11:07:18
Ahhh right, I see what happens here now - it loads the "to" page body instead of the actual page body. A header location redirect with a 301 response code would be much more SEO friendly.
Comment by SebCls
2009-01-04 04:48:40
Is there a redirect action ?
I'm moving from wikini to wikkawiki.
On Wikini there is an action for this.
Here is the code modified for Wikka
%%(php)
<?php
/*
redirect.php : Permet de faire une redirection vers une autre pages Wiki du site

Copyright 2003 Eric FELDSTEIN
Copyright 2003 David DELON
Copyright 2004 Jean Christophe ANDRE
Copyright 2009 Sebastien CELLES
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/*
Parameters : page : WikiName of the page where you want to redirect
example : {{redirect page="HomePage"}}
*/

//get parameters
//$redirPageName = $this->GetParameter("page");
foreach ($vars as $param => $value)
{
switch ($param)
{
case 'page':
$redirPageName=$value;
break;
}
}


if (empty($redirPageName)){
echo $this->Format("//Missing \"page\" parameter.//");
}elseif ($this->GetMethod() == 'show'){
if (eregi("^".$redirPageName."$",$this->GetPageTag())){
echo $this->Format("//A page can't redirect to itself.//");
}else{
$fromPages = isset($_COOKIE['redirectfrom']) ? explode(":",$_COOKIE['redirectfrom']) : array();
if (in_array($this->GetPageTag(),$fromPages)){
echo $this->Format("//Circular redirection.//");
} else {
$fromPages[] = $this->GetPageTag();
SetCookie('redirectfrom', implode(":",$fromPages), time() + 30, $this->CookiePath);
$this->Redirect($this->Href('', $redirPageName));
}
}
}else{
echo '<span style="color: red; weight: bold">Redirecting to "' . $this->Link($redirPageName) . '"</span>';
}
?>
%%
Comment by BrianKoontz
2009-01-10 00:11:59
Nothing as of the moment other than what has been documented on this page. Thanks for contributing the code!
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki