Forum Moderators: coopster

Message Too Old, No Replies

correcting 404's with PHP

$REQUEST_URI is blank?

         

davidpbrown

12:01 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



In an effort to correct 404's for upper case and underscores, I have htaccess suggesting
ErrorDocument 404 /error/notfound.php

and notfound.php as
<?php
$request = $_SERVER['$REQUEST_URI'];
$location = strtolower($request);
$newlocation = strtr($location, "_", "-");
if ($newlocation==$request) {
header('Location: [mysite.com...]
} else {
$newlocation = "http://www.mysite.com" + $newlocation;
header('Location: $newlocation');
}
?>

BUT it's not working.. REQUEST_URI is apparently blank.. (I stuck a file write in there to find out what was happening)

Is it recommended to use PHP for 404 correction's and, if so, can you suggest what might be wrong?

Thanks

bcolflesh

1:01 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you try:

$request = $REQUEST_URI;
echo $request;

Is it still blank?

Does your .htaccess file have any Rewrite rules?

davidpbrown

1:27 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



$request = $REQUEST_URI; crashes out I expect because $REQUEST_URI is undefined.

echo $request; doesn't do anything for me.. I did think of that but couldn't see what it did (I'd expected output to screen)
so instead opted for
$file="test.txt";
$fp = fopen($file,"a");
fwrite($fp, "\r\n=?");
fwrite($fp, $request);
fwrite($fp, "\r\n=>");
fwrite($fp, $newlocation);
fclose($fp);

which I take to be foolproof.. and it does indeed output what I'd expect.. so does the filter.. it's just that the input seems to be blank.

This originally from a suggestion by ggrot in thread [webmasterworld.com...] to use PHP_SELF but that generates only /notfound.php i.e. the location of the php file itself not the requested URI.

There's no conflicting Redirects in the htaccess as I removed them all.. this is all inside a temp directory while I'm trying it.

I wondered if this might be because it's two steps.. one through .htaccess

Do we know, has anyone else used PHP for 404 correction that I could see example?

ams_david

2:04 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



$request = $_SERVER['$REQUEST_URI'];

$ on REQUEST_URI is not necesarry. Try it this way.:

$request = $_SERVER['REQUEST_URI'];

bcolflesh

2:20 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ams_david is correct - I missed that - but my example works fine with PHP 4.3.2 - as I am doing exactly what you asking about - must be the difference of our server configs.

davidpbrown

2:35 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



Excellent .. thankyou ams_david

<?php
$request = $_SERVER['REQUEST_URI'];
$newrequest = strtolower($request);
$newrequest = strtr($newrequest, "_", "-");
if ($newrequest==$request) {
header('Location: [mysite.com...]
} else {
$newrequest = "Location: [mysite.com".$newrequest;...]
header($newrequest);
}
?>

and now it works and I am happy :)