A
A
Artuha2014-05-07 08:23:18
1C-Bitrix
Artuha, 2014-05-07 08:23:18

Setting 404 in Bitrix?

Hello!
Site on Bitrix. The problem is this: the 404 page is shown only on pages of the form: site.ru/abrakadabra
And on site.ru/catalog/abrakadabra instead of 404 it shows the contents of site.ru/catalog, although in ChromeDevTools the 404th header ...
At the root lies the 404.php file is correctly configured, I tried to add it to init.php

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

function _Check404Error()
{
   if (defined('ERROR_404') && ERROR_404=='Y' && !defined('ADMIN_SECTION'))
   {
   GLOBAL $APPLICATION;
   $APPLICATION->RestartBuffer();
   include   $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.SITE_TEMPLATE_ID.'/header.php';
   require   ($_SERVER['DOCUMENT_ROOT'].'/404.php');
   include   $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.SITE_TEMPLATE_ID.'/footer.php';
   }
}

but it did not help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MintTea, 2014-05-07
@MintTea

In general, checking (defined('ERROR_404') && ERROR_404 == 'Y') does not catch the occurrence of a 404 error in the handler. I wrote to technical support about this, they transferred my ticket to development. It was in October. While they are slow chatting, you can move the problematic component into your namespace, find where CHTTP::SetStatus is called in it, and add the desired line after it:

CHTTP::SetStatus('404 Not found');
defined('ERROR_404') or define('ERROR_404', 'Y');

Or, you can compare the $arResult that is passed to the template when visiting /catalog/ and when visiting /catalog/abrakadabra, and find a condition that would allow you to determine that an error has occurred. In my opinion, there is something with TEMPLATE_NAME. Then add something like this to index.php in the template:
if ($arResult['TEMPLATE_NAME'] !== 'index.php')
{
    defined('ERROR_404') or define('ERROR_404', 'Y');
}

Or, if you don't want to do this, you can modify the CHTTP::SetStatus method itself, something like this:
class CHTTP
{
    public static function SetStatus($status)
    {
        ... 
        if ($status === '404 Not found')
           defined('ERROR_404') or define('ERROR_404', 'Y');
    }
}

But this is not welcomed by Bitrix, and will crash with every update.

M
Misha Vasilyev, 2014-05-07
@vasilyev

This is a very painful issue of Bitrix. I at one time, after reading this topic, scored and made a handler with LocalRedirect. There is a solution in the body of the topic that allows you to set a 404 status and save the URL in the address bar, but then the header.php of the topic is included twice, in my case it was unacceptable.

S
Sergey, 2015-09-27
@Logic87

Add this code to /bitrix/php_interface/init.php

define("PREFIX_PATH_404", "/404.php");

AddEventHandler("main", "OnAfterEpilog", "Prefix_FunctionName");

function Prefix_FunctionName() {
    global $APPLICATION;

    // Check if we need to show the content of the 404 page
    if (!defined('ERROR_404') || ERROR_404 != 'Y') {
        return;
    }

    // Display the 404 page unless it is already being displayed
    if ($APPLICATION->GetCurPage() != PREFIX_PATH_404) {
        header('X-Accel-Redirect: '.PREFIX_PATH_404);
        exit();
    }
}

Your 404.php should look like this:
<?
if ($_SERVER['DOCUMENT_URI'] == "/404.php") {
    $_SERVER['REQUEST_URI'] = $_SERVER['DOCUMENT_URI'];
}
include_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/urlrewrite.php');

CHTTP::SetStatus("404 Not Found");
@define("ERROR_404","Y");

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");

$APPLICATION->SetTitle("Страница не найдена");
?>
<h1>Страница не найдена</h1><br>
<p>Вы набрали неправильный адрес страницы. Пожалуйста, перейдите на главную страницу сайта или воспользуйтесь формой поиска.</p>
<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?>

For catalog items, this is sufficient. But for non-existent sections, you need to add to the template of the complex component of the Catalog /bitrix/templates/s1/components/bitrix/catalog/new_catalog/bitrix/catalog.section.list/.default/template.php after the first line:
if(!$arResult["VARIABLES"]["SECTION_ID"]){
CHTTP::SetStatus("404 Not Found");
@define("ERROR_404","Y");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question