Answer the question
In order to leave comments, you need to log in
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';
}
}
Answer the question
In order to leave comments, you need to log in
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');
if ($arResult['TEMPLATE_NAME'] !== 'index.php')
{
defined('ERROR_404') or define('ERROR_404', 'Y');
}
class CHTTP
{
public static function SetStatus($status)
{
...
if ($status === '404 Not found')
defined('ERROR_404') or define('ERROR_404', 'Y');
}
}
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.
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();
}
}
<?
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");?>
if(!$arResult["VARIABLES"]["SECTION_ID"]){
CHTTP::SetStatus("404 Not Found");
@define("ERROR_404","Y");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question