solution for .php braking millions of links to our pages?

Jason Bainbridge jaseone at myrealbox.com
Wed Feb 5 14:05:25 UTC 2003


On Wed, 5 Feb 2003 21:17, Neil Stevens wrote:
> On Wednesday February 05, 2003 05:07, Rob Kaper wrote:

> > $redirectURL = "";
> > if (strstr($REQUEST_URI, ".html"))
> > 	$redirectURL = str_replace(".html", ".php", $REQUEST_URI);
> >
> > if ($redirectURL)
> > {
> > 	header("Location: $REQUEST_URI);
> > 	exit();
> > }
> >
> > to the top of our 404 handling page.
>
> Won't work.  You don't check for the existance of the php file, leading to
> a confusing 404 of a php file of someone honestly requests a non-existant
> html file.

Okay well how about this...

function url_header($url) {
        $url = preg_replace('#^http:#i', '', $url);
        if (preg_match('#^//#', $url)){
                list(,,$domain, $file) = explode('/', $url, 4);
                $file = "/$file";
        } else {
                $file = $url;
                if (! preg_match('#^/#', $file)) $file = "/$file";
                $domain = $_SERVER[HTTP_HOST];
        }
        $fid = fsockopen($domain, 80);
        if (! $fid) return "Host not responding";
        fputs($fid, "HEAD $file HTTP/1.0\r\nHost: $domain\r\n\r\n");
        $head = fread($fid, 4096);
        fclose($fid);
        return $head;
}

function url_exists($url) {
        return preg_match('#^HTTP/.*\s+200\sOK\s#i', url_header($url));
}
$redirectURL = "";
if (strstr($REQUEST_URI, ".html"))
        $redirectURL = str_replace(".html", ".php", $REQUEST_URI);
if ($redirectURL && url_exists($redirectURL)){
header("location:$redirectURL");
exit();

Unfortunately there isn't a native url_exists function in PHP so I took the 
above from a comment at:

http://www.php.net/manual/en/function.file-exists.php 

It seems to work fairly well at my end and most importantly it is transparent 
to the users, the script does unfortunately however add some small overhead 
but it only requests the header of the php file to see if it exists so I 
think that is acceptable for transitional purposes.

Regards,
Jason




More information about the kde-www mailing list