views:

157

answers:

4

Hi

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.

A: 

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

var_dump($_SERVER);

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

Philippe Gerber
Done it. Nothing helpfule. I mean, mentioning, b.php.
Wbdvlpr
What if you try "ErrorDocument 404 /my_404.php" (without the host)?
Philippe Gerber
+1  A: 

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:

Jonathan Sampson
I dont want to check log files. I just want to check it when I am on 404 page. I am not trying to record the data twice.
Wbdvlpr
+2  A: 

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.

Matt Bridges
REQUEST_URI shows current page .. my_404.php
Wbdvlpr
@Matt .. yes it works greate now. Thank you for helping me out. @steef also answered the same. so which I should mark as answer!!
Wbdvlpr
+2  A: 

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.

Steef
I was just editing my answer to add that...upvoted
Matt Bridges