Skip to content

Instantly share code, notes, and snippets.

@aktaumag
Last active March 16, 2022 12:46
Show Gist options
  • Save aktaumag/36c45babde04c1b3565cb607c78b8a3c to your computer and use it in GitHub Desktop.
Save aktaumag/36c45babde04c1b3565cb607c78b8a3c to your computer and use it in GitHub Desktop.
$ws_uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$ws_uri_protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$ws_uri_canonical = $ws_uri_protocol . $_SERVER['HTTP_HOST'] . $ws_uri_parts[0];
@aktaumag
Copy link
Author

aktaumag commented Sep 19, 2018

CMS Bitrix

Отличная статья с видео инструкцией по настройке основной категории и канонических адресов для товаров, находящихся сразу в нескольких категориях: https://dev.1c-bitrix.ru/learning/course/?COURSE_ID=34&LESSON_ID=7199


bitrix/templates/имя-шаблона/header.php

// START WebSEO.kz Michael Nossov: 
$wsasset = \Bitrix\Main\Page\Asset::getInstance();
$wspath = $request->isHttps() ? 'https://' : 'http://';
$wscanonical = $wspath . $_SERVER['HTTP_HOST'] . str_replace('index.php', '', $APPLICATION->GetCurPage(true));
$wspagenum = '';
if(isset($_REQUEST['PAGEN_1']) && !empty($_REQUEST['PAGEN_1']) && intval($_REQUEST['PAGEN_1']) > 1){
    $wscanonical .= '?PAGEN_1='.intval($_REQUEST['PAGEN_1']);
    // Если эта страница с пагинацией, то добавляем в Title и Description фразу "Страница 2", "Страница 3" и т.д. (кроме первой страницы)
    $wspagenum = ' → Страница '.intval($_REQUEST['PAGEN_1']);
    if(!empty($APPLICATION->GetProperty('description', ''))){
        $wsdesc = $APPLICATION->GetProperty('description');
        $APPLICATION->SetPageProperty('description', $wsdesc.$wspagenum);
    }
}
// Если изначально нет у страницы канонического адреса, то добавляем свой
if(empty($APPLICATION->GetProperty('canonical', ''))){
    $APPLICATION->SetPageProperty('canonical', $wscanonical);
}
// END WebSEO.kz

bitrix/templates/имя-шаблона/components/bitrix/system.pagenavigation/.default/template.php

// START WebSEO.kz Michael Nossov:
$wsasset = \Bitrix\Main\Page\Asset::getInstance();
$wspath = $request->isHttps() ? 'https://' : 'http://';
$wsuri = $wspath . $_SERVER['HTTP_HOST'] . str_replace('index.php', '', $APPLICATION->GetCurPage(true));
if ($arResult["NavPageNomer"] != 1) {
    // если это не первая страница, то добавляем метатег rel=prev
    // только к первой не добавляется окончание ?PAGEN_1=1
    $wspprev = '';
    if($arResult["NavPageNomer"] != 2){
        $wspprev = '?PAGEN_'.$arResult["NavNum"].'='.($arResult["NavPageNomer"]-1);
    }
    $wsasset->addString('<link rel="prev" href="' . $wsuri . $wspprev.'">');
}
if($arResult["NavPageNomer"] != $arResult["NavPageCount"]) {
    // если это не последняя страница, то добавляем метатег rel=next
    $wsasset->addString('<link rel="next" href="' . $wsuri . '?PAGEN_'.$arResult["NavNum"].'='.($arResult["NavPageNomer"]+1).'">');
}
// END WebSEO.kz

@aktaumag
Copy link
Author

aktaumag commented Feb 1, 2019

WebaSyst

В админке находим шаблон и в файле index.html добавляем перед стандартным выводом тега небольшую модификацию

    {$trimmed_absolute_url = $wa->currentUrl(true, true)}
    {if empty($canonical)}
        {if $wa->get('page', 1) > 1}
            {$canonical_http = $wa->currentUrl(true)}
        {else}
            {$canonical_http = $trimmed_absolute_url}
        {/if}
    {else}
        {$canonical_http = $canonical}
    {/if}
    {if !empty($canonical_http)}
        {$canonical = $canonical_http|regex_replace:'/^http:\/\//':'https://'}
    {/if}

    {if !empty($canonical)}
        <link rel="canonical" href="{$canonical}"/>
    {/if}

@aktaumag
Copy link
Author

aktaumag commented Jan 20, 2020

// Перенаправление на страницу без index.php
$ws_uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
if(substr($ws_uri_parts[0], -10) == '/index.php'){
header($_SERVER['SERVER_PROTOCOL'] . " 301 Moved Permanently");
header("Location: ".$wscanonical);
exit();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment