18

To make a long story short, I have dynamic pages on a website that display reviews. If there are no reviews associated with a particular city/county/area/etc the mysql query returns 0 rows which triggers the following code:

if (!$validRevQuery) {
    header("HTTP/1.0 404 Not Found");
    include("http://{$PDS['site']}/404.php?request=".urlencode($_SERVER['REQUEST_URI']));
    exit;
}

On some webhosts this triggers a "URL file-access is disabled" error. Which is fine, but on the ones that allow URL file-access, the 404 file is included and properly displayed. I changed the code slightly to display an absolute path like so:

if (!$validRevQuery) {
    header("HTTP/1.0 404 Not Found");
    $_GET['request'] = urlencode($_SERVER['REQUEST_URI']);
    include($_SERVER['DOCUMENT_ROOT']."/404.php");
    exit;
}

And now, it's giving me the generic "Oops! This link appears to be broken." error page. (I have google toolbar, so this may be different depending upon browser and plugins). No idea why this is happening, so any help is appreciated!

Yev
  • 1,921
  • 8
  • 25
  • 44
  • Does setting `$_GET['request']` work? Not something I've ever tried to do. Can you not just call `$_SERVER['REQUEST_URI']` from within 404.php? I'd have thought it would work, as only one URI has been requested by the web server. I believe Google stuff shows that generic message when the provided 404 content is too small (not sure the exact character length, Google it ;) ) – chigley Oct 19 '10 at 15:43
  • Setting a `GET` variable sounds like bad practice in any way. – Pekka Oct 19 '10 at 15:44
  • setting the GET variable was just a quick workaround, and yes I have done it before and it works. I will try to find an alternate solution to that. :) – Yev Oct 19 '10 at 15:50
  • don't forget that `$_SERVER["REQUEST_URI"]` will be available in `404.php` if you include it the way you show in your second example! So chances are you won't need to store it anyway. – Pekka Oct 19 '10 at 15:53

2 Answers2

42

Your problem has nothing to do with what you include: It's that your page is too small.

In my experience, Chrome's built-in "Oops" page is displayed, like the one in Internet Explorer, when the page emits only the 404 header and less than a defined number of bytes of content (I think it's 512 bytes in IE, don't know the limit in Chrome).

I tend to pad my 404 pages with a few hundred bytes of meaningless content wrapped in HTML comments to make sure the custom 404 page is displayed.

Or of course, use the opportunity for some cool ASCII Art!

   <!--                            oooo   ooo
                                   $   $  $   $
                                   "o  $ $  o""
                                     o  "   "ooooo
                                 oo ""           o$
                   o            o            oo  "
                  $$             $o$""$o  ooo$
                  $"$          o $    "$  $
        o$o       $ "$         $ $     $ $
         $$$o     $$ "$       o$ $     o $o
         "$ ""o   "$   "o     $$ "o     o"
     $o   $$   "o  $     "oo  $"  $   o$"
      "$   $o    "o$$       "o$    $o$" oo$
        "o "$o                     "$o $"$$
          "        oo$$$$$$$oo        $oo$$""      o" o
 """""""""      o$$$$$$$$$$$"$o             o"""$o$$  o$
       ooo$$$"o$$$$$$$$$$$$$$ "$o    o   o$$$o   $ $ o$
    o$$$$$$$$$$$$$$$$$$$$$$$$    "oo  o      ""o  "$ $
   $$$$$$$$$$$$$$$$$$$$$$$$$$      "$o   o$$"""$     " oo""o
o""""$$$$$$$$$$$$$$$$$$$$$$"         ""$o"$o          "   o$
     "$$$$$$$$""""$""$$$""              "$oo$""$o     o$"""
      $$$$$$$"                           $""""$"  o""""
       $"""""$ooooo        ooooo$$$$$$$     o$" o"
        $     """" oooo$$$$$$$$$$$$$$"     $"  o"
      oo$   oooo$$$$$$$$$$$"""""$$$$"    o$" o$"
    "$ $o$$$$$$$$$$$$$""$     o$$$"oooo$"  o"
      "o$ "$$$$$$$$$$$$         $$o$"$$$   $"
        "$  ""$$$$$$$$$        o$"$$$ "$$o$$
          "o   ""$$$$$$o     o$$$$ ""$o """$
            "$o    ""$$$$$$o"  o$$$$oo o$$$$
               ""$oo     $$" "$$$"" ooooooo$
                    """"$"  o$"   oo$$$$$""$$
                       $ oo$"  o$$$$$""  ooo$
                       $o$"  o$$$$"  oo$$$$$$$o
                        $$ o$$$"  o$$$$$$"""""$o
                         "o$$"  o$$$$""  o$$$$$$$o
                           "$oo$$$$"  o$$$$$""" o$o
                             "$$$" oo$$$"" oo$$$$$$$
                          ooooo$oo$$$"" oo$$$$"""$$""
                         $"oooo $$$" o$$$$""      $
                       o$"o$   $$"oo$$""       " o$
                       $ o$$o  $$o$$"          oo$$
                       $ $$$$  $$$$$$$$$$$$$$$$$$$$
                       $ $$$$  $$$$$$$$$$$$$$$$$$$$"
                       $ $$$$  $$$$$$$$$$$$$$$$$$$$
                       $ ""    ""$$$$$$$$$"""$""""
                       $o         $"$"    " $"
                        $o       $$  $o    o$
                         "$o   o$$    ""$$$"
                           """"""  -->
Pekka
  • 418,526
  • 129
  • 929
  • 1,058
  • 7
    The custom 404 content needs to be larger than 512 bytes to not be ignored. I'd do 2K of meaningless data just to be safe. Many browsers do this. – Brad Oct 19 '10 at 15:45
  • Woah, that makes a lot of sense. 404.php had an error right at the top, and since the error message was shorter than, say 512 bytes - it would not display anything at all. Thank you! – Yev Oct 19 '10 at 15:52
  • 5
    Note to people wondering why this doesn't work for them: this solution doesn't work in Firefox if your error page has no `Content-Length` header, but instead relies on `Connection: close` to terminate the response. – RichieHindle Jul 20 '11 at 10:16
-4

Trying redirecting using a 307 or 301 with a location header

MANCHUCK
  • 2,269
  • 1
  • 15
  • 21