Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grab missing page?

Status
Not open for further replies.

youradds

Programmer
Joined
Jun 27, 2001
Messages
817
Location
GB
Hi. I'm trying to write a page that will intelligently figure out what page the 404 error came from, and report an appropriate link.

In .htaccess I have;

ErrorDocument 404
..and in test.php, I have;

Code:
foreach ($_GET as $name => $val)  {
  echo &quot;$name => $val <BR>&quot;;
}

foreach ($_POST as $name => $val)  {
  echo &quot;$name => $val <BR>&quot;;
}

foreach ($GLOBALS as $name => $val)  {
  echo &quot;$name => $val <BR>&quot;;
}


foreach ($HTTP_ENV_VARS as $name => $val)  {
  echo &quot;$name => $val <BR>&quot;;
}

Does anyone know how to get the URL for the missing file into a variable?

TIA.

Andy
 
You have several options - the easiest has nothing to do with PHP but with Apache. If you run Apache use an SSI:
Code:
<!--#echo var=&quot;REQUEST_URI&quot; -->

With PHP the same info is available from the $_SERVER['REQUEST_URI'] superglobal array (PHP <4.1.0 uses $HTTP_SERVER_VARS instead).
 
>>>With PHP the same info is available from the $_SERVER['REQUEST_URI'] superglobal array (PHP <4.1.0 uses $HTTP_SERVER_VARS instead).<<<

Yeah, this is the kind of thing I'm looking at. The problem is that it comes up with the redirect URL, rather than the original i.e the 404 page points to test.php, and the REQUEST_URI comes up with test.php, even when trying to goto something.html, which doesn't exist.

Any more ideas?

Cheers

Andy
 
For Apache there should be also:
Code:
$_ENV['REDIRECT_URL']
Can you find that?
 
Those 3 options return;


/test.php

(note the blank value)

.. with the following code;

echo $_SERVER['HTTP_HOST'] . &quot; <BR> &quot; . $SERVER['SERVER_NAME'] . &quot; <BR> &quot; . $_SERVER['REQUEST_URI'];

If it does what I need, it would would at least return the dead file name (i.e rubbish.html ).

Any more ideas guys? Thanks for the effort so far. You can see why I've been getting so annoyed with it all :(

Cheers

Andy
 
The blank value I can explain: typo.
The underscore is missing from $_SERVER['SERVER_NAME']

I'll ponder a bit more...
 
>>>The underscore is missing from $_SERVER['SERVER_NAME']<<<

Still gives a blank value.

>>>I'll ponder a bit more... <<<

Thanks :)

Andy
 
Just tested the .htaccess directive with Apache 1.3.24 and PHP 4.2.0 on my local Windows machine.
The requested (non-existing) page is included in the vars I mentioned before.

What server are you using and what is the OS?
 
Your problem most likely lies in the fact that you use a full URL in your Apache directive. That causes Apache to send a Location header back to the client, requesting the error page rather than Apache doing an internal redirect where [tt]REQUEST_URI[/tt] would be present and contain the expected value.

//Daniel
 
Daniel is right.
It is stated in the Apache documentation that external redirects will not have the named server variables available. This is the case even when you redirect to the same server using http requests.
Good catch.
 
The problem is, if I enter the full path, it doesn't work correctly (gives a standard IE page error). Code is;

ErrorHandler 404 /home/ace-ins/home/public_html/new/index.php

Any ideas?

Cheers

Andy
 
You need to specify the path from the apache document root. You have the full file path specified.
Use
Code:
ErrorHandler 404 /new/index.php
 
It works great... thanks :)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top