Search code examples
phpapache.htaccesshttp-status-code-404superglobals

Get page referer and missing page that generates Error 404 using PHP


I've been researching online for a way to retrieve the URL that generates a 404 error. I've found various postings on this subject in this and other sites and tried them all. Unfortunately I'm still not being able to get what I want. I will try to explain what I've done and what I'm getting.

SCENARIO:

I have a page. Let's call http://www.example.com/index.php.

In this page, I have a link, to let's say wrong_page.php or, if you prefer, http://www.example.com/index.php.

As the page wrong_page.php doesn't really exists, When someone click on this link, it redirects to error404.php. (to make it easier, all pages are in the root directory.

Question: Is it possible for error404.php to give me both index.php as the original page where the wrong link resides and wrong_page.php as the wrong link itself?

SETUP

At the root folder of my site I have .htaccess file with the following directive:

ErrorDocument 404 error404.php

Inside error404.php I loop through the whole $_SERVER variable and, this is what I get:

  1. If the visitor clicks on the wrong_link (the one that leads to wrong_page.php), I get $_SERVER['HTTP_REFERER'] = 'index.php' but no reference to wrong_page.php;
  2. If the visitor types wrong_page.php in the address bar, $_SERVER['HTTP_REFERER'] doesn't even exist (tested with isset() function).

Moreover, in both cases $_SERVER['REQUEST_URI'] = 'error404.php'

I tried all $SERVER global variables starting with "REDIRECT" as mentioned in other postings, but they do not existed (also tested with isset() function).

Also, I tried this in two different servers: my own instance of WAMP (that I use for development) and a production server running apache.

In my computer, when I look at server error logs (apache_error.log), I find a line like this:

[Wed Jul 23 11:28:51 2014] [error] [client 127.0.0.1] File does not exist: /www.example.com  referer: /www.example.com/index.php

In the production server, this same line does not get created when I reproduce the same error.

What am I missing? Or is it all that I can get? Are there any server parameters I can change to get what I want? Is there another global variable I should look at? As I said, I know there are many other postings regarding this, but they all talk about these same global variables and, as I said, they don't give me what I want (wrong_page.php as the culprit).


Solution

  • I finally found the "solution" if I can call it a solution.

    The whole problem was in the URL used in the directive inside .htaccess.

    I wrote my directive as being:

    ErrorDocument 404 error404.php
    

    But inside my actual .htaccess I had the directive as

    ErrorDocument 404 http://www.example.com/error404.php
    

    The presence of http://www.example.com was the problem. All I had to do was make the directive as follow:

    ErrorDocument 404 /error404.php
    

    This way, $_SERVER['REQUEST_URI'] gives me the wrong URI requested by the user. If it is a link in my site, it will give me the page where the link exists in $_SERVER['HTTP_REFERER']. Otherwise, if the user simply typed the wrong URI in the address bar, $_SERVER['HTTP_REFERER'] will be blank.

    I hope this helps others with the same issue.