Forum Moderators: phranque

Message Too Old, No Replies

How to capture the file request that triggers an error 404

want to pass/store the url that triggers a 404 in a form or session var

         

benlieb

6:27 am on Jan 27, 2005 (gmt 0)

10+ Year Member



Does anyone know a good way to capture into a variable (I tend to program in php) the url that triggers an error 404?

This seems like it should be easy, but I can't figure it out. Does apache have this capability?

adb64

7:57 am on Jan 27, 2005 (gmt 0)

10+ Year Member



benlieb,

PHP has some reserved variables from which you can retrieve the requested page.
Use $SERVER_NAME to get www.mydomain.com and use $REQUEST_URI for the requested page, e.g. /non-existing-page.html.
Also see [php.net...]

Regards,

Arjan

benlieb

1:36 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



I know how to use these vars to get the requested page when that exists! For example if I load page.php I can use:

$thisDir = $_SERVER['PHP_SELF'];
$thisFile = basename ($thisDir);

but when apache gets a request for a page that doesn't exist, it automatically sends back the default 404 page, which is an .html page. Even if I cutomized it to be a .php page, how would I get the ORIGINAL requested page that triggered the 404 page to be passed to it as a var? Or more simply, how can I get apache to pass a variable to the 404 page, letting that page know what was originally requested. Something like

/404.php?orignal_request=missing_file.php

What I want to do is perform some string operations on the original (not found) file to offer some recommendations to the user as to how to get to similar files in the site.

Ideas anyone?

jdMorgan

3:08 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In mod_rewrite, the environment variable %{THE_REQUEST} contains the entire request line sent by the client browser, unaffected by any rewrites or subsequent processing, i.e. "GET /pagename HTTP/1.1".

I'm not sure about the scope of this variable, but it may be possible to pass it out of mod_rewrite and capture it in your "recommendations" script.

Jim