12

I have a custom 404 page which works fine except for the message I want to display on this page.

I would like it to say the url of the page which can't be found but instead it displays the url of the 404 page.

Here's what I have...

You were looking for <?php echo $_SERVER['REQUEST_URI'] ?>.

The htaccess file contains the line: ErrorDocument 404 /404/

Simon
  • 1,980
  • 14
  • 21
Tom
  • 12,776
  • 48
  • 145
  • 240
  • 2
    How have you set up the error page? (Are you, for example, using Apache's `ErrorDocument` directive, or ?) – John Parker Apr 14 '11 at 12:49
  • I have an htaccess file with the line: `ErrorDocument 404 /404/` - and the 404 page has the line included above. – Tom Apr 15 '11 at 22:08
  • 1
    can you post the contents of `` from an instance when you get this behaviour? I'm confused as to why none of these approaches work. I experimented a bit with Apache and couldn't produce a situation where *none* of them got the result you want. – Simon Apr 21 '11 at 13:15
  • @Simon143 - thanks for the advice. It would appear that I need to use DOCUMENT_URI. although as you say, I really would have expected my original code to work. Odd!! – Tom Apr 21 '11 at 22:44

5 Answers5

12

You need to use $_SERVER['HTTP_REFERER'] instead - that will be the address they requested first.

This only works in the exact case described in the question - where the browser has actually been redirected to the 404 page. In that situation, $_SERVER['REQUEST_URI'] contains the URI of the 404 page rather than the originally requested page as described.

Using Apache's ErrorDocument 404 /handle404.php in the site config or .htaccess would mean that $_SERVER['REQUEST_URI'] would actually work, but a more robust solution is the option in the update below.

Update:

Apparently $_SERVER['REDIRECT_URL'] might be a better bet however, having searched around a bit.

For both cases, as mentioned by the commenters below, bear in mind that any headers are just as prone to malicious content as $_POST, $_GET and others, so process them before outputting anything.

Update 2:

Didn't see the post from @Janoz below - he correctly mentions REDIRECT_URL.

Simon
  • 1,980
  • 14
  • 21
  • 1
    There's no guarantee this will be provided, as it's a HTTP header sent from the browser. (In essence, there should be a guaranteed server-side solution to this problem.) – John Parker Apr 14 '11 at 12:50
  • 3
    Don't forget to do some escaping.. Technically, the referrer is user input. – svens Apr 14 '11 at 12:51
7

From the perspective of the php page, that really is the request uri. Showing the error page is done by the webserver. Apache for example will add some extra server variables. REDIRECT_URL is probably the one you are looking for.

Janoz
  • 953
  • 4
  • 9
  • 1
    I thought this was going to work, but it didn't... still displays the URL of the 404 page (404.php for example) – Tom Apr 18 '11 at 15:57
1

I did not write this function but it is what I use to do the same thing:

    function selfURL() {
    $s = empty($_SERVER["HTTPS"]) ? ''
        : ($_SERVER["HTTPS"] == "on") ? "s"
        : "";
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? ""
        : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
    return substr($s1, 0, strpos($s1, $s2));
}

then to print it:

<?php print(selfURL()); ?>
George Reith
  • 13,132
  • 18
  • 79
  • 148
0

If a page doesn't exist you redirect him to the 404 page? Idealy, I would display the 404 directly on the page which wasn't found. This way, you don't have to redirect, and you can correctly use REQUEST_URI. And the code for your 404 can still be centralized!

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
  • I don't redirect them, I display the 404 directly on the page not found. See my comment on the original post for the htaccess code – Tom Apr 15 '11 at 22:11
0

use file_exists to check whether the file your user is looking for exists or not. if it doesn't the redirect them to a custom made error page.

Arjun Bajaj
  • 1,932
  • 7
  • 24
  • 41