php header for 404's

Discussion in 'PHP' started by dugu, Jul 12, 2009.

  1. #1
    So, here is my problem.
    I have a phpld script running.
    In httacces, everything is redirected to index.php
    I want to implement custom 404 errors, so I have to handle them in index.php
    The code for handling is this:


    // Check page request, if not valid a http header 404 is sent
    $http_status = (!empty ($_REQUEST['httpstatus']) ? intval ($_REQUEST['httpstatus']) : request_status());


    if ($http_status == 404)
    {
    header("HTTP/1.1 404 Not Found");
    header("Location: http://www.mygreencorner.com/error404.php");
    exit();
    }

    The problem is that when I call the last header function, I am implicitly setting the header to 302, thus destroying the whole purpose of the error pages.
    What's the fix on this?
    THANKS
     
    dugu, Jul 12, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    er, try:

    header("Location: http://www.mygreencorner.com/error404.php", 404);
    PHP:
    ;
     
    dimitar christoff, Jul 12, 2009 IP
    dugu likes this.
  3. dugu

    dugu Active Member

    Messages:
    1,381
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Hi David, thanks for replying.
    Unfortunately, it is not working:
    http://it.php.net/manual/en/function.header.php
    Any other toughs on this?
     
    dugu, Jul 13, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    who's david? yes

    http://it.php.net/manual/en/function.header.php#78470

    seems you need a 'true' as well:
    <?php
    // 301 Moved Permanently
    header("Location: /foo.php",TRUE,301);
    
    // 302 Found
    header("Location: /foo.php",TRUE,302);
    header("Location: /foo.php");
    
    // 303 See Other
    header("Location: /foo.php",TRUE,303);
    
    // 307 Temporary Redirect
    header("Location: /foo.php",TRUE,307);
    ?>
    PHP:
    i see no reason why you can't use a 404 as well - and yes - i have been using a 301 redirect this way instead of a 302 (default one being sent by php).

    good luck :)
     
    dimitar christoff, Jul 13, 2009 IP
  5. dugu

    dugu Active Member

    Messages:
    1,381
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Ohh, sorry , I must be so tired I can't even read. I meant Dimitar, thank you I really appreciate this :)
    Thanks. But now I am in more fog that I was at the beginning. Actually, I am testing with http://web-sniffer.net
    Now, I get the good status, and web-sniffer even show that the location is:
    Location: http://www.mysite.com/error_page404.php
    But in my web browser, I still have the http://www.mysite.com/gsdfgdgfdgdfgdf page, and the usual error page you see from google toolbar, instead of my custom one.
     
    dugu, Jul 13, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    try to remove the header("HTTP/1.0 404 Not Found"); ? also - do you have any Errordocument directives in htaccess?
     
    dimitar christoff, Jul 13, 2009 IP
  7. dugu

    dugu Active Member

    Messages:
    1,381
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    90
    #7
    no, I have commented that already. (header("HTTP/1.0 404 Not Found");)
    I think the htaccess does come into equation, as we are already at the part of executing php. Htaccess is run before this, all queries are forwarded to index.php, and in here we can play. I don't have any ErrorDocument in htaccess, but I already tried with and without, with no success. :(
     
    dugu, Jul 13, 2009 IP