08.08.2024 Have you read it yet 5372 once It helped 2 to the readers

Adding the rel=canonical tag to the pages of the Bitrix website

Task:

Add an tag on the website rel="canonical" to remove duplicates in the Catalog and Information pages.

  • For detailed catalog pages, we will use the typical 1C-Bitrix functionality through component settings and the information block.
  • For all other cases, we will use our own code.

Step 1. We display the canonical tag on the detailed product page.

The website where we performed this task had duplicates of the same product, although the product itself was physically linked to the same subsection in the information block. Here is an example of duplicates of one product in the catalog structure.

  • /catalog/instrument-dlya-montazha/zaklyepochniki/2187/
  • /catalog/instrument-dlya-montazha/2187/
  • /catalog/ruchnoy_instrument/2187/

First of all, we perform a simple task, i.e. we go to the settings of the Information Block and Component settings and follow the recommendations described on the official 1C-Bitrix website.: https://dev.1c-bitrix.ru/learning/course/?COURSE_ID=34&LESSON_ID=7199.

Next, we add a line to the code of the complex component "DETAIL_SET_CANONICAL_URL" => "Y",

Let's check what we've got!

Now, when you go to the pages indicated in our example, a permanent tag is displayed in the site code, which indicates correct the path to this product

< link rel=" canonical" href="https://SITE.RU/catalog/instrument-dlya-montazha/zaklyepochniki/2187/" />

Now you can go to the self-written code for the entire site, except for the product card.

Step 2. Write custom code to output the canonical tag for all other cases

What will this code do?:

  • Generate a base URL without a port and with HTTPS.
  • Clear the current URL from /index.php and filter parameters, if necessary.
  • Add / at the end of the URL If it 's not .php or .html.
  • Exclude Product ID pages from the canonical tag and generates the correct canonical URL for the remaining pages.

Code description

  1. Defining the base URL:
    $protocol = 'https://'; // HTTPS is used $host = $_SERVER['HTTP_HOST']; $host = preg_replace('/:\d+$/', '', $host); $baseUrl = $protocol . $host;
    
    • Defining the protocol (https://) and the site domain ($_SERVER['HTTP_HOST']).
    • Removing the port from the domain, if present (preg_replace('/:\d+$/', '', $host)).
    • Creating the base URL of the website ($baseUrl).
  2. Getting the current URL for the canonical tag:
    $canonicalUrl = $APPLICATION->GetCurPage(true);
    $canonicalUrl = preg_replace('/\/index\.php$/', '', $canonicalUrl);
    
    • We get the current page URL using the method $APPLICATION->GetCurPage(true).
    • We're cleaning up /index.php from the end of the URL, if present.
  3. Adding smart filter page processing in the Catalog:
    if (strpos($canonicalUrl, '/filter/') !== false) {
        $parentSectionUrl = preg_replace('/\/filter\/.*$/', '', $canonicalUrl);
        $parentSectionUrl = preg_replace('/\/index\.php$/', '', $parentSectionUrl);
        $canonicalUrl = $parentSectionUrl;
    }
    
    • If the URL contains /filter/, we process it to get the URL of the parent section (without filter parameters).
    • We're cleaning up /index.php from the parent URL, if present.
  4. Adding / to the end of the URL:
    if (!preg_match('/\.(php|html)$/', $canonicalUrl)) {
        $canonicalUrl = rtrim($canonicalUrl, '/') . '/';
    }
    
    • Checking that the URL ends with / if it does not end at .php or .html.
  5. We add verification to the detailed product page and output the canonical tag.:
    if (!preg_match('/\/\d+\/$/', $canonicalUrl)) {
        $canonicalFullUrl = $baseUrl . $canonicalUrl;
        if ($baseUrl . $_SERVER['REQUEST_URI'] !== $canonicalFullUrl) {
            echo '';
        }
    }
    
    • We check whether the current page is a detailed product page based on the availability of the product ID in the URL (preg_match('/\/\d+\/$/', $canonicalUrl)).
    • Creating the full canonical URL and adding it to <link rel="canonical"> if it differs from the current page URL.
    • We check whether the current page is a detailed product page based on the availability of the product ID in the URL (preg_match('/\/\d+\/$/', $canonicalUrl)).
    • Creating the full canonical URL and adding it to <link rel="canonical"> if it differs from the current page URL.
  6. The finished code must be placed before the closing tag. head (file header.php in the site template)

    This code will help you correctly generate the canonical URL for your site, taking into account the specific conditions for filters and detailed product pages.

Feedback

Was the material useful?

The assessment helps to understand which materials are worth developing and supplementing.

Discuss the task

Ask for help

Describe the task and leave your contacts. I will look at the introductory notes and contact you in a convenient way.

Direction *