Ben Muircroft Ben Muircroft - 25 days ago 9
Apache Configuration Question

.htaccess redirect 404 but remember the mistake as an url variable

My .htaccess file is one line

ErrorDocument 404 https://example.com/folder/404.php?url=%{HTTP_REFERER}


if I go to https://example.com/folder/derp.php (a non-existent page)

It literally takes me to:

https://example.com/folder/404.php?url=%{HTTP_REFERER}

whereas it should take me to:

https://example.com/folder/404.php?url=https://example.com/folder/derp.php

why is
%{HTTP_REFERER}
being treated as a string?




RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://www.example.com/folder/page.php.*
RewriteRule .* https://example.com/folder/404.php?url=%{HTTP_REFERER}


tried that the HTTP_REFERER is blank

Answer Source

%{HTTP_REFERER} is mod_rewrite variable that cannot be used in ErrorDocument.

You will have to use a mod_rewrite rule instead of ErrorDocument if you want to pass %{HTTP_REFERER}.

Another alternative is to not use %{HTTP_REFERER} at all and use slightly different ErrorDocument directive as this:

ErrorDocument 404 /folder/404.php

and get the original 404 causing URI using:

$_SERVER["REQUEST_URI"]