TigerTiger
TigerTiger

Reputation: 10806

how to log which page was "not found"? (Trying custom error page with htaccess, php, http_referer)

I was just wondering how to get the link of the webpage which was not found on my website.

With following .htaccess code, when a non-existing page is requested, user is redirected to my_404.php.

Options -Indexes

ErrorDocument 404 http://mysite.com/my_404.php

If I have to track down what was the link of the page, which was not found, how do I do that??

Just for example, A.php is a valid web page, while b.php is not. So if I am on A.php and try to view b.php (non-existent page) .. .htaccess redirects me to my_404.php on which I see HTTP_REFERER as A.php .. but what I am looking for is that "somebody tried to view B.php". How do I do that?

Thanks for your help.

EDIT

Please see: I dont want to check in log files. I am asking about something on page. Thanks.

Upvotes: 2

Views: 494

Answers (4)

Matt Bridges
Matt Bridges

Reputation: 49385

The variable you are looking for is called $_SERVER['REQUEST_URI']. It holds the original request that was made to your server by the client.

You can see a list of the other "$_SERVER" variables here.

Edit:

After a bit of google searching, your problem is that your error page is specified with the http:// qualifier. Change your htaccess so that the ErrorDocument is specified as a local file, e.g.:

ErrorDocument 404 /www/my_404.php 

After you do that, $_SERVER['REQUEST_URI'] should hold the correct value.

Upvotes: 2

Steef
Steef

Reputation: 34665

When you use a remote ErrorDocument URL like http://mysite.com/my_404.php, Apache will send a redirect to that URL to the client, that's why you can't get the URL that causes the 404 from $_SERVER['REQUEST_URI'] like Matt Bridges suggested. You might want to try using a local ErrorDocument (no scheme and servername), like this:

ErrorDocument 404 /my_404.php

I suspect $_SERVER['REQUEST_URI'] will then return the originally requested URL.

Upvotes: 2

Sampson
Sampson

Reputation: 268344

This is likely already being done in your Apache log file. You could just parse the data rather than recording it twice.

Stackoverflow Archives:

Additional Relevant Content:

Upvotes: 2

Philippe Gerber
Philippe Gerber

Reputation: 17836

Have a look at your $_SERVER array in my_404.php

var_dump($_SERVER);

I guess you'll find what you need. :-)

Upvotes: 0

Related Questions