A
A
Alexander2021-02-01 21:51:44
1C-Bitrix
Alexander, 2021-02-01 21:51:44

How to set up redirect after changing the CNC for subsections of the bitrix directory?

The redirect for the product card works, but the redirect for sections and subsections redirects to the same page, I can’t understand what the problem is ....

On the site, you need to change the CNC for the catalog.
Previously set #SECTION_CODE_PATH#/#ELEMENT_CODE#/
which changes to #SECTION_CODE#/#ELEMENT_CODE#/.
I tried to do it as described here
1. In the SEF_URL_TEMPLATES parameter, I added the line
"redirect" => "#SECTION_CODE_PATH#/#ELEMENT_CODE#/",
2. In my catalog complex component template, I created the redirect.php file (where files like section.php, element.php, etc.)
3. In this file I wrote:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) die();?>
<?$this->setFrameMode(true);?>
<?
    use Bitrix\Main\Loader;
    use Bitrix\Main\ModuleManager;
    Loader::includeModule("iblock");
    Loader::includeModule("highloadblock");
    global $OptimusSectionID;
    $arElement = array();
    if($arResult["VARIABLES"]["ELEMENT_ID"] > 0){
            $arElement = COptimusCache::CIBLockElement_GetList(array('CACHE' => array("MULTI" =>"N", "TAG" => COptimusCache::GetIBlockCacheTag($arParams["IBLOCK_ID"]))), array("IBLOCK_ID" => $arParams["IBLOCK_ID"], "ACTIVE"=>"Y", "ID" => $arResult["VARIABLES"]["ELEMENT_ID"]), false, false, array("ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "DETAIL_PAGE_URL", "NAME"));
    }
    elseif(strlen(trim($arResult["VARIABLES"]["ELEMENT_CODE"])) > 0){
            $arElement = COptimusCache::CIBLockElement_GetList(array('CACHE' => array("MULTI" =>"N", "TAG" => COptimusCache::GetIBlockCacheTag($arParams["IBLOCK_ID"]))), array("IBLOCK_ID" => $arParams["IBLOCK_ID"], "ACTIVE"=>"Y", "=CODE" => $arResult["VARIABLES"]["ELEMENT_CODE"]), false, false, array("ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "DETAIL_PAGE_URL", "NAME"));
    }
    //$OptimusSectionID = $arSection["ID"];
    
    if($arElement['DETAIL_PAGE_URL']) {
        LocalRedirect($arElement['DETAIL_PAGE_URL']);
      }

global $OptimusSectionID;
    $arSection = array();
    if($arResult["VARIABLES"]["SECTION_ID"] > 0){
            $arSection = COptimusCache::CIBlockSection_GetList(array('CACHE' => array("MULTI" =>"N", "TAG" => COptimusCache::GetIBlockCacheTag($arParams["IBLOCK_ID"]))), array('GLOBAL_ACTIVE' => 'Y', "ID" => $arResult["VARIABLES"]["SECTION_ID"], "IBLOCK_ID" => $arParams["IBLOCK_ID"]), false, array("ID", "IBLOCK_ID", "UF_TIZERS", "IBLOCK_TYPE_ID", "IBLOCK_SECTION_ID", "CODE", "SECTION_ID", "SECTION_PAGE_URL", "NAME"));
    }
    elseif(strlen(trim($arResult["VARIABLES"]["SECTION_CODE"])) > 0){
            $arSection = COptimusCache::CIBlockSection_GetList(array('CACHE' => array("MULTI" =>"N", "TAG" => COptimusCache::GetIBlockCacheTag($arParams["IBLOCK_ID"]))), array('GLOBAL_ACTIVE' => 'Y', "=CODE" => $arResult["VARIABLES"]["SECTION_CODE"], "IBLOCK_ID" => $arParams["IBLOCK_ID"]), false, array("ID", "IBLOCK_ID", "UF_TIZERS", "IBLOCK_TYPE_ID", "IBLOCK_SECTION_ID", "CODE", "SECTION_ID", "SECTION_PAGE_URL", "NAME"));
    }
        $OptimusSectionID = $arSection["ID"];

            if(strpos($arSection['SECTION_PAGE_URL'], '#SECTION_CODE_PATH#') !== true) {
                $elementId = $arResult['VARIABLES']['SECTION_ID'];
                    $filter = ['IBLOCK_ID' => $arParams['IBLOCK_ID'], 'ID' => $elementId];
                        $element = [];
                            $iterator = CIBlockSection::GetList([], $filter, false, false, ["IBLOCK_ID", "ID", "SECTION_PAGE_URL", "IBLOCK_TYPE_ID", "IBLOCK_SECTION_ID", "CODE", "SECTION_ID", "NAME"]);

                            if($row = $iterator->GetNext()) {
                                $element = $row;
                            }
        if($element['SECTION_PAGE_URL']) {
            LocalRedirect($element['SECTION_PAGE_URL']);
        }
    }
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
PetrPo, 2021-02-02
@Alexander_AC

here is the answer, if you need a redirect only for elements
If you need a redirect for sections, as in the TS question, you can add a check

$variables['SECTION_CODE_PATH'] = $arResult['VARIABLES']['SECTION_CODE_PATH'].'/'.$arResult['VARIABLES']['ELEMENT_CODE'];
CIBlockFindTools::checkSection($arParams['IBLOCK_ID'], $variables);

if(isset($variables['SECTION_ID']) && $variables['SECTION_ID']) {
  // здесь для разделов редирект
}
else {
  // здесь для элементов
}

The rest is in the comments.

A
Alexander, 2021-02-02
@Alexander_AC

The solution to the question is a very prompt response and comments from user PetrPo .
Thank you so much PeterPo !

P
Pavel, 2022-04-20
@Asokr

Thanks Petrpo.
I'll leave here the finished, working code from the dialog above. Sections and elements are redirected.
In my case, the old URLs had underscores, the new ones had dashes...+ 301 redirect instead of 302...

<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
$this->setFrameMode(true);

$variables['SECTION_CODE_PATH'] = $arResult['VARIABLES']['SECTION_CODE_PATH'].'/'.$arResult['VARIABLES']['ELEMENT_ID'];
$variables['SECTION_CODE_PATH'] = str_replace('_', '-', $variables['SECTION_CODE_PATH']);

CIBlockFindTools::checkSection($arParams['IBLOCK_ID'], $variables);

if(isset($variables['SECTION_ID']) && $variables['SECTION_ID']) {
  $iblock = \Bitrix\Iblock\IblockTable::getList([
    'select' => ['SECTION_PAGE_URL'],
    'filter' => ['ID' => $arParams['IBLOCK_ID']],
    'cache' => ['ttl' => 31536000]
  ])->fetch();

  if(strpos($iblock['SECTION_PAGE_URL'], '#SECTION_CODE_PATH#') !== false) {
    $sectionId = $variables['SECTION_ID'];

    $filter = ['IBLOCK_ID' => $arParams['IBLOCK_ID'], 'ID' => $sectionId];


    $obCache = new CPHPCache();
    if($obCache->InitCache(36000, serialize($filter), '/iblock/catalog/redirect')) {
      $section = $obCache->GetVars();
    }
    elseif($obCache->StartDataCache()) {
      $section = [];

      $iterator = CIBLockSection::GetList([], $filter, false, ["IBLOCK_ID", "ID", "SECTION_PAGE_URL", "IBLOCK_TYPE_ID", "IBLOCK_SECTION_ID", "CODE", "SECTION_ID", "NAME"]);

      if(defined("BX_COMP_MANAGED_CACHE")) {
        global $CACHE_MANAGER;
        $CACHE_MANAGER->StartTagCache('/iblock/catalog/redirect');

        if($row = $iterator->GetNext()) {
          $section = $row;
          $CACHE_MANAGER->RegisterTag('iblock_id_'.$arParams['IBLOCK_ID']);
        }

        $CACHE_MANAGER->EndTagCache();
      }
      else {
        if($row = $iterator->GetNext()) {
          $section = $row;
        }
      }

      $obCache->EndDataCache($section);
    }
    if($section['SECTION_PAGE_URL']) { 
      LocalRedirect($section['SECTION_PAGE_URL'], false, '301 Moved permanently');
    }
  }
} else {
  $iblock = \Bitrix\Iblock\IblockTable::getList([
    'select' => ['DETAIL_PAGE_URL'],
    'filter' => ['ID' => $arParams['IBLOCK_ID']],
    'cache' => ['ttl' => 31536000]
  ])->fetch();

  if(strpos($iblock['DETAIL_PAGE_URL'], '#ELEMENT_CODE#') !== false) {
    $elementId = $arResult['VARIABLES']['ELEMENT_ID'];
    $filter = ['IBLOCK_ID' => $arParams['IBLOCK_ID'], 'ID' => $elementId];
    
    $obCache = new CPHPCache();
    if($obCache->InitCache(36000, serialize($filter), '/iblock/catalog/redirect')) {
      $element = $obCache->GetVars();
    }
    elseif($obCache->StartDataCache()) {
      $element = [];

      $iterator = CIBLockElement::GetList([], $filter, false, false, ['IBLOCK_ID', 'ID', 'DETAIL_PAGE_URL']);
      
      if(defined("BX_COMP_MANAGED_CACHE")) {
        global $CACHE_MANAGER;
        $CACHE_MANAGER->StartTagCache('/iblock/catalog/redirect');

        if($row = $iterator->GetNext()) {
          $element = $row;
          $CACHE_MANAGER->RegisterTag('iblock_id_'.$arParams['IBLOCK_ID']);
        }

        $CACHE_MANAGER->EndTagCache();
      }
      else {
        if($row = $iterator->GetNext()) {
          $element = $row;
        }
      }
        
        
      $obCache->EndDataCache($element);
    }


    if($element['DETAIL_PAGE_URL']) {
      LocalRedirect($element['DETAIL_PAGE_URL'], false, '301 Moved permanently');
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question