R
R
Roman2014-12-25 13:07:50
1C-Bitrix
Roman, 2014-12-25 13:07:50

How to add a canonical link to 1C Bitrix?

There is a site (Internet store), it has a lot of filtering options, and so is the division into pages (pagination) The
site.ru/catalog/category/?PAGEN_1=2
question is, how can I implement a canonical link in the page header?

<link rel="canonical" href="site.ru/catalog/category/">

The problem is that the search engines started claiming I have twice as many pages as I actually do (they started creating duplicates of the same pages). There is only one way out, to specify the canonical page, but how to implement it?
I've looked all over the internet and haven't found an answer. Who faced, how did you decide?
-----------------------
Problem solution:
For catalog.section
in result_modifier.php
$arSection = CIblockSection::GetById($arResult["ID"])->GetNext();
$arResult['SECTION_PAGE_URL'] = $arSection['SECTION_PAGE_URL'];
$cp = $this->__component; 
if (is_object($cp))
$cp->SetResultCacheKeys(array('SECTION_PAGE_URL'));

in component_epilog.php
$APPLICATION->AddHeadString('<link href="http://'.SITE_SERVER_NAME.$arResult['SECTION_PAGE_URL'].'" rel="canonical" />',true);

For catalog.element
in result_modifier.php
$arElement = CIblockElement::GetById($arResult["ID"])->GetNext();
$arResult['DETAIL_PAGE_URL'] = $arElement['DETAIL_PAGE_URL'];
$cp = $this->__component; 
if (is_object($cp))
$cp->SetResultCacheKeys(array('DETAIL_PAGE_URL'));

in component_epilog.php
$APPLICATION->AddHeadString('<link href="http://'.SITE_SERVER_NAME.$arResult['DETAIL_PAGE_URL'].'" rel="canonical" />',true);

Thanks to everyone who responded. I hope this solution helps someone like me.
ps technical support agreed with me that this is a necessary functionality and promised that they would add it to a future version, though they did not specify which release.
ps guys, I don’t follow the question (it is in demand, comments constantly appear), the solution may be outdated. Write in the LC the correct option so that I can update the solution in the question.

Answer the question

In order to leave comments, you need to log in

9 answer(s)
K
Knebel, 2015-03-29
@Knebel

Alternatively, you can add the following code between the tags to the /bitrix/templates/theme_name/header.php file <head>...</head>:

<link rel="canonical" href="<? echo $APPLICATION->GetCurDir(); ?>" />

R
Roman Yamchuk, 2017-02-13
@tomgif

$APPLICATION->SetPageProperty('canonical', $arResult["CANONICAL_PAGE_URL"]);

R
Roman, 2015-04-02
@RGBPlus

Solution added to the topic itself!

S
Seo5, 2016-10-08
@Seo5

I struggled with this task for 4 hours today and before that the same amount, it didn’t work out for the categories according to your scheme, for those who also didn’t succeed, I still found a solution ( saitsozdanie.ru/bitrix/kak-ubrat-dubli -stranits-sa... - displays canonical on all pages at once, who may need it

X
xavikz, 2016-12-21
@xavikz

But wouldn't such code dynamically change on different pages of the same product?
Let's say for a url with the address:
directory/manufacturer/name
there will be a canonical link = "catalog/manufacturer/name"
and for a
directory/country/name
there will be a canonical link ="catalog/country/name"
So what's the point of such canons?
After all, they should point all duplicates to one page, but in fact this does not happen.

D
Denis Gorokhov, 2018-05-08
@gorokhov_dv

Made a new module for 1C-Bitrix.
Allows you to beautifully process pagination, create pages like /page-2/, /stranica-2/ and at the same time comply with all Google recommendations for pagination, add rel=next/prev/canonical tags to pages with a page!
I hope it will be useful for someone!
I'd be grateful for your feedback and suggestions!
Module link: marketplace.1c-bitrix.ru/solutions/gorokhovdv.paging

D
Dmitry Bogdanov, 2019-10-09
@DmitBogdanov

  • Proper inclusion of canonical for directory items - here
  • Enabling canonical for parent catalog categories: add
    to /bitrix/templates/template/components/bitrix/catalog.section.list/template/component_epilog.php
  • Enabling canonical for child categories of the catalog: add
    to /bitrix/templates/template/components/bitrix/catalog.section/template/component_epilog.php

M
Mikhail Alekseev, 2019-04-19
@helicopter-windmill

I found a solution for inserting the canonical tag for all pages of the site:
- find the bitrix/modules/main/include/prolog.php file
- between the lines:
- put it in there

$url = $_SERVER['REQUEST_URI'];
$url = explode('?', $url);
$url = 'http://www.site.ru'.$url[0];
echo '<link rel="canonical" href="'.$url.'"/>';

Z
ZardoZAntony, 2019-11-06
@ZardoZAntony

AddHeadString is a bad idea. And if you already have another crookedly written module installed canonical through the same function. It is correct to do this:
In the admin panel, create the canonical property. We set it in the right places via $APPLICATION->SetPageProperty('canonical', 'link'); And the meaning of this delayed function is that only the last link in the code will be installed, and there can be any number of installations in any components and there will be no conflict.
in init.php we add deferred functions for output

function ShowCanonical() {
    global $APPLICATION;
    $APPLICATION->AddBufferContent('GetCanonical');
}

function GetCanonical(){
    global $APPLICATION;
    $val = $APPLICATION->GetProperty('canonical');
    if ($val)
        return  '<link rel="canonical" href="' . $val . '">';
}

Output via ShowCanonical();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question