Search code examples
phpapache.htaccesshttp-referer

How to find referrer URL while using htaccess 404 redirect?


I've used the code below to create a custom 404 message (page not found) using a .htaccess file.

RewriteEngine On
ErrorDocument 404 https://%{HTTP_HOST}/404.php

It works well, but I want to know what was the wrong URL that caused this redirection. In other words, I need to fetch HTTP referer while user is in 404.php. So I have used $_SERVER['HTTP_REFERER'] in 404.php, but it does not return anything.
Where did I have mistakes? And what is your solutions to solve this problem?


Solution

  • Remove http:// from 404 handler and have it like this:

    ErrorDocument 404 /404.php
    

    The URL that is causing 404 is available to you in 404.php using:

    $_SERVER["REQUEST_URI"]
    

    When http:// ise used in 404 handler server performs full redirection and you loose original REQUEST_URI.