'https://www.google.com/search?q=$1',
  'w'   => 'https://en.wikipedia.org/wiki/Special:Search?go=1&search=$1',
  '_default'   => 'https://www.google.com/search?q=$1',
));
SDVA($HandleActions, array('dtrt'=>'HandleDTRT'));
SDVA($HTMLHeaderFmt, array('DTRT-opensearch'=>""));
function HandleDTRT($pagename, $auth="read") {
  global $DTRT_links, $WikiTitle, $EnableRedirect, $Charset;
  #will ask for password if needed
  $page = RetrieveAuthPage($pagename,$auth,1, READPAGE_CURRENT);
  if (preg_match_all("/^\\s*(\\S*)\\s*:[^\\S\n]+(\\S*)/m", $page['text'],
    $match, PREG_SET_ORDER)) {
    foreach($match as $m)
    {
      $tmp = explode(',', $m[1]);
      foreach($tmp as $key)
        $DTRT_links[$key] = $m[2];
    }
  }
  list($k, $q) = explode(' ', urldecode(stripmagic($_GET['q'])), 2);
  
  if($k == "opensearch") {
    $url = PageVar($pagename, '$PageUrl');
    $xmltpl = <<
  DTRT {$WikiTitle}
  Do the right thing custom search
  
  UTF-8
EOF;
    header("Content-type: application/opensearchdescription+xml");
    echo $xmltpl;
    exit;
  }
  elseif ($k == "setdefault") {
    $q = trim($q);
    if($q) {
      setcookie('DTRT_default', $q);
      $GLOBALS['MessagesFmt'][] = "Default redirection set to '$q'";
    }
    else {
      setcookie ("DTRT_default", "", time() - 3600);
      $GLOBALS['MessagesFmt'][] = "Default redirection reset";
   }
   return HandleBrowse($pagename, 'read');
  }
  
  if(!isset($DTRT_links[$k]) || $DTRT_links[$k] == 'null' || $DTRT_links[$k] == '' ) {
    $rxfound = 0;
    foreach ($DTRT_links as $rx=>$u) {
      if (!preg_match('/^\\/.*\\/i?$/', $rx)) continue;
      if (! preg_match($rx, $k)) continue;
      $rxfound = 1;
      $k = $rx; # no 'break', later defs override earlier ones
    }
    if(!$rxfound) {
      $qd = @$_COOKIE["DTRT_default"];
      if ($qd && @$DTRT_links[$qd]) {
        $k = $qd;
      }
      else $k = '_default';
      $q = stripmagic($_GET['q']);
    }
  }
  $where = $DTRT_links[$k];
  if(strpos($where, 'function:')===0 ) {
    $func = str_replace('function:', "DTRT_", $where);
    if(! function_exists($func))
      Abort("User function '$func' does not exist.");
    $url = $func($q);
  }
  else {
     $url = str_replace('$1', rawurlencode($q), $where);
     $url = str_replace('$2', rawurlencode(utf8_decode($q)), $url);
  }
  if(!$url) Abort("Bad configuration, where='$where', key='$k'; query='$q'.");
  if($url{0} == ',') {
    $url = substr($url, 1);
    $EnableRedirect = 0;
  }
  if(strpos($url, '&&')) {
    $opt = ParseArgs(str_replace('&&',' ', $url),'(?>(\\w+)[=])');
    $frm = "
    
    
    Redirecting...
    ";
    if(IsEnabled($EnableRedirect, 1))
      $frm .= "";
    $frm .= "";
    die($frm);
  }
  $url = PUE($url);
  Redirect($pagename, $url);
}