Спасибо PetrPo.
Оставлю тут готовый, рабочий код. Происходит редирект разделов и элементов.
<?
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');
}
}
}