F
F
fiter2015-08-31 05:36:49
PHP
fiter, 2015-08-31 05:36:49

How to give 404 error in Bitrix?

There is an infoblock with a list of subdomains.
If you go to a subdomain that is not in this list, the status 404 should be given.
The code below works on the entire site, except for the main page.
For example, a.vsesvetodiody.ru/dileram.html returns 404 correctly, but just a.vsesvetodiody.ru gives a blank page, although the script definitely reaches the creation of the 1111111111111111111.txt file
. What could be the problem?

AddEventHandler("main", "OnEpilog", "handler404");
function handler404()
{
  
    global $USER;
    global $APPLICATION;
  
    $curDir = $APPLICATION->GetCurDir();
    $arDir = explode('/', $curDir);
    $curPage = $APPLICATION->GetCurPage();
    
    if (isset($_SERVER["REAL_FILE_PATH"]))
    {
        $arRequest = explode("/", $_SERVER["REDIRECT_URL"]);
        if  (
                (
                    ($_SERVER["REAL_FILE_PATH"] == "/index.php") 
                    && (count($arRequest) != 3)
                ) 
                || 
                (
                    (count($arRequest) == 3) 
                    && (strlen($arRequest[2]) > 0) 
                    && (substr($arRequest[2], -5, 5) != ".html")
                )
            )
        {
            define("ERROR_404", "Y");
        }
    }


    $poddomen=str_replace(".vsesvetodiody.ru","",$_SERVER["HTTP_HOST"]);
    $poddomen=str_replace("vsesvetodiody.ru","",$poddomen);
   
  
    if(defined('ERROR_404') && ERROR_404 == 'Y')
    {
    
        $template = 'main';
        $APPLICATION->RestartBuffer();
        $APPLICATION->SetPageProperty("title", "Ошибка 404");
        $APPLICATION->SetPageProperty("h1", "Error 404");
        $APPLICATION->SetTitle("Error 404");
        include_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/urlrewrite.php');
        include $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.$template.'/header.php';
        include $_SERVER['DOCUMENT_ROOT'].'/include/404_text.php';
        include $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.$template.'/footer.php';
    
    } else if ($poddomen) {

        CModule::IncludeModule('iblock');
        $select = Array("NAME","ID","CODE");
        $arFilter = Array("IBLOCK_ID"=>2, "CODE" => $poddomen, "ACTIVE" => "Y");
        $res = CIBlockElement::GetList(Array("SORT"=>"ASC", "PROPERTY_PRIORITY"=>"ASC"), $arFilter, $select);
        $poddomen = $res->GetNext();  

        if(!$poddomen["ID"])  {

          file_put_contents('1111111111111111111.txt', 1); 

          $template = 'main';
          $APPLICATION->RestartBuffer();
          $APPLICATION->SetPageProperty("title", "Ошибка 404");
          $APPLICATION->SetPageProperty("h1", "Error 404");
          $APPLICATION->SetTitle("Error 404");
          include_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/urlrewrite.php');
          include $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.$template.'/header.php';
          include $_SERVER['DOCUMENT_ROOT'].'/include/404_text.php';
          include $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.$template.'/footer.php';

        }

    }
  
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-08-31
Madzhugin @Suntechnic

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