View Single Post
  #14  
Old August 18th, 2008, 10:26 AM
lch's Avatar

lch lch is offline
General
 
Join Date: Feb 2007
Location: R'lyeh
Posts: 3,861
Thanks: 144
Thanked 403 Times in 176 Posts
lch is on a distinguished road
Default Re: Automated redirect for old post/thread links

Richard, thanks for looking into this. I'd recommend that you create substitutes for the old showflat.php, shownested.php etc. scripts instead of abusing 404 error pages for this.

I'll drop some PHP code here which I have been using for some projects myself. First, a replacement for PHP's parse_url function:
PHP Code:
// returns an array with the following elements defined in it:
//   scheme://username:password@host:port/path?query#fragment
// this function is more robust than parse_url

function parseUrl($url) {
  
$r  '!(?:(?<scheme>\w+)://)?(?:(?<username>\w+)\:(?<password>\w+)@)?(?<host>[^/:]+)?';
  
$r .= '(?:\:(?<port>\d*))?(?<path>[^#?]+)?(?:\?(?<query>[^#]+))?(?:#(?<fragment>.+$))?!i';
  
preg_match($r$url$out);
#  for ($i = 0; $i < 9; ++$i)
#    unset($out[$i]);
  
return $out;

then one which does the reverse:
PHP Code:
// inverse function to parseUrl

function glueUrl($parsed)
{
  if (!
is_array($parsed)) return false;
  
$uri strlen($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '' '//') : '';
  
$uri .= strlen($parsed['user']) ? $parsed['user'].(strlen($parsed['pass']) ? ':'.$parsed['pass'] : '').'@' '';
  
$uri .= strlen($parsed['host']) ? $parsed['host'] : '';
  
$uri .= strlen($parsed['port']) ? ':'.$parsed['port'] : '';
  if (
strlen($parsed['path'])) {
    
$uri .= ($parsed['path'][0] == '/') ? $parsed['path'] : ('/'.$parsed['path']);
  }
  
$uri .= strlen($parsed['query']) ? '?'.$parsed['query'] : '';
  
$uri .= strlen($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
  return 
$uri;

and finally something which uses both to merge a given URL with a query string, which shows how they can be used:
PHP Code:
// merges given URL with the specified query string

function MergeQueryStrings($url$query) {
  
$parsed parseUrl($url);
  
parse_str($query$addq);
  
parse_str($parsed['query'], $oldq);
  
$newq array_merge($oldq$addq);
  
$parsed['query'] = http_build_query($newq);
  return 
glueUrl($parsed);

Maybe it's some help. What you really should make use of is the parse_str function.
__________________
Come to the Dom3 Wiki and help us to build the biggest Dominions-centered knowledge base on the net.
Visit my personal user page there, too!
Pretender file password recovery
Emergency comic relief
Reply With Quote