如何获取Not Found页面的URL,以便在自定义404页面中显示它?

如何获取Not Found页面的URL,以便在自定义404页面中显示它?

问题描述:

Ok, I have a issue with my Webserver, when I try using

<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ?>

It turns up the file it's self: Eg. blah.com/404.php

Also how would I do this with a 403 page? I know its possible and I've tried

    $_SERVER['HTTP_REFERER']

but noting turns up, or sometimes it's just a zero. This is my first time with a 404, what am I doing wrong?

I found the Issue, You must post a relative path, not a direct link, otherwise it will redirect to that page instead of showing the 404.

Eg:

 ErrorDocument 404 /404.php

That way the .htacess doesn't redirect to Yoursite.com/notfound.php It will also send a 404 (Not Found) instead of a 200 (OK)

A 404 status code should be returned by the url which is "not found". So instead of redirecting to another php file you should 'return' this code instantly.

You should have a look at error document handling in .htaccess files. Something like this may help you:

ErrorDocument 404 /notfound.php

This will return a 404 status code. The good thing for this solution: you can access $_SERVER['REQUEST_URI'] in your notfound.php file and use it to display an error message or any other action.