A
A
a91050513962020-11-03 19:56:50
1C-Bitrix
a9105051396, 2020-11-03 19:56:50

Bitrix redirect?

Can you tell me if it is possible to make a redirect after changing for cards from #ELEMENT_ID# to #ELEMENT_CODE# ? I've already broken my head.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PetrPo, 2020-11-03
@a9105051396

1. Add your own string to the SEF_URL_TEMPLATES parameter 2. In your catalog complex component template, create a redirect.php file (where files like section.php, element.php, etc.) 3. Write in this file
"redirect" => "#SECTION_CODE_PATH#/#ELEMENT_ID#/",

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

$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']);
  }
}

4. in the same place where the redirect.php file was created, there is a .parameters.php file, add at the end
$arTemplateParameters['SEF_URL_TEMPLATES_redirect'] = array(
    'PARENT' => 'SEF_MODE',
    'NAME' => "Редирект",
    "DEFAULT" => "",
    "VARIABLES" => array(),
);

this is so that when saving the component from the public, your parameter is not erased, well, it's better to 'NAME' => "Redirect", through GetMessage do

P
Pavel, 2022-04-20
@Asokr

Thanks Petrpo.
I'll leave here the finished, working code. Sections and elements are redirected.

<?
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