A
A
Andrew2018-02-27 21:47:56
1C-Bitrix
Andrew, 2018-02-27 21:47:56

How to implement 404 error in bitrix?

I understand that this question has been discussed many times, but I have not found an answer to it.
I set up the components and on non-existent pages I get a 404 header (in developer tools).
The problem is that the check in init.php doesn't work.
I assume that the 404 header is obtained later, I tried different hooks, but it does not work.
If I just clear the entire browser cache, then I get 404 (get_headers), but when I refresh the page, I get 503 (get_headers).
Can you tell me what is my mistake?

AddEventHandler("main", "OnBeforeEndBufferContent", "ChangeMyContent"); // и так не работает OnEpilog

function ChangeMyContent(&$content)
{
       //Так проверею ответ
  print_r(get_headers('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));

  if(defined('ERROR_404') && ERROR_404 == 'Y') {
    $content = file_get_contents($_SERVER["DOCUMENT_ROOT"].'/bitrix/templates/.default/404.php');
                return;
  }
  CHTTP::SetStatus('404 Not Found');
}

I repeat, if you look at the headers received when loading the page, there is a 404, but how to catch it in init.php?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
I'm Yoda, 2018-02-27
@Kaspel

Try like this in init.php

AddEventHandler("main", "OnEpilog", "_Check404Error", 1);

function _Check404Error() {
    if (defined("ERROR_404") && ERROR_404 == "Y" || CHTTP::GetLastStatus() == "404 Not Found") {
        global $APPLICATION;
        $APPLICATION->RestartBuffer();
        require $_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/header.php";
        require $_SERVER["DOCUMENT_ROOT"] . "/404.php";
        require $_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/footer.php";
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question