As a senior CMS website operation personnel in a security company, I am well aware of the importance of content optimization and SEO-friendliness to the success of a website.The canonical URL is an indispensable part of SEO strategy, which can effectively resolve duplicate content issues and ensure that search engines correctly identify the authoritative version of the page.When developing templates and publishing content using AnQi CMS, we often need to flexibly handle these details.

Understanding Canonical URL and Its Importance

Canonical URL (规范链接) is an HTML<link>tag, whichrelproperty is set to"canonical"

Set Canonical URL in AnQiCMS

The AnQi CMS provides good support for Canonical URL in the content management aspect.When adding or editing documents in the background, you can find a field named "Standard Link" in the "Other Parameters" section.Here, you can specify a fully qualified specification URL for the current document.This means you can manually define the authoritative address of this page, even if there may be other forms of link paths within the site.http://orhttps://The complete link at the beginning, ensuring it points to the final preferred version of the content. If not set, the Anqi CMS will usually generate one based on the default URL structure of the page.

Use{% tdk %}Tagging Canonical URL

In the template system of AnQi CMS,{% tdk %}Tags are a powerful tool used to dynamically retrieve the TDK (Title, Description, Keywords) information of a page, as well as including the standard link (Canonical URL). When we want to display the page's<head>When outputting the link of the area specification, you can use this tag.

The basic calling method is as follows:

<link rel="canonical" href="{% tdk with name="CanonicalUrl" %}" />

However, there may be a problem with this direct call: if the backend has not set a standard link for the current page,{% tdk with name="CanonicalUrl" %}This will return an empty string. This may lead to generating an empty<link rel="canonical" href="" />tag, although usually harmless, it is best to avoid it from the perspective of code tidiness and rigor.

How to determine if the Canonical URL exists and perform conditional output

To ensure that the corresponding output is only made when there is an actual Canonical URL value<link>Tags, we should introduce a judgment mechanism in the template.The template engine of AnQi CMS supports variable assignment and conditional judgment, making it simple and elegant to determine if a link exists.

Firstly, we can use{% tdk %}assign the Canonical URL value obtained by the tag to a temporary variable. Then, through a{% if %}Conditional judgment, check if this variable is empty. If the variable contains a valid URL, then output the complete<link rel="canonical">Label.

The following is a recommended template code snippet that demonstrates how to determine if a standard link exists and to output conditionally:

{%- tdk canonical with name="CanonicalUrl" %}
{%- if canonical %}
<link rel="canonical" href="{{canonical}}" />
{%- endif %}

In the above code:

  • {%- tdk canonical with name="CanonicalUrl" %}This line of code will attempt to retrieve the TDK settings from the current page.CanonicalUrland assign its value to a template variable namedcanonical.{%-The use is to eliminate the whitespace before and after the label line, keeping the HTML output neat.
  • {%- if canonical %}is a conditional judgment statement. IfcanonicalThe variable has been successfully assigned and its value is not empty (i.e., the background has set a standard link), thenifthe content within the block will be rendered.
  • <link rel="canonical" href="{{canonical}}" />: This is the actual HTML tag for outputting the standard link, itshrefThe value of the attribute comes fromcanonicala variable.
  • {%- endif %}: The end tag for conditional judgment, also uses-%}to avoid whitespace.

This way, we ensure that the HTML of the page only includes the corresponding link when there is indeed a defined specification.<link rel="canonical">Tags, it avoids outputting unnecessary or empty tags, making the template code more robust and professional.This is not only a good coding habit, but also further improves the search engine optimization quality of the website.

Frequently Asked Questions

How will Safe CMS handle it if I have not set the Canonical URL?If you have not explicitly set the standard link in the background content management, Anqi CMS will usually use the actual URL of the current page as the default standard link.This means that, without additional configuration, the search engine will still consider the current accessed URL as the standard version.However, manually setting the specification link can better control the authoritative version, especially in cases of content duplication or parameterized URLs.

Does the Canonical URL only apply to document pages? Can I set a canonical link for category pages or single pages?According to the backend function design of Anqi CMS, the canonical URL is mainly set in the 'Other Parameters' section of 'Add Document' or 'Edit Document'.This means that it is mainly targeted at document content such as articles or products.{% tdk %}Tags can be retrievedCanonicalUrlBut the backend may not provide a direct setting entry to independently define them.In this case, the system's default URL structure usually acts as the standard URL, or you may need to customize development to expand this feature.

Why do I need to make a judgment in the template?canonicalDoes the variable exist, rather than output directly{% tdk with name="CanonicalUrl" %}?The existence of the variable is to avoid generating empty or incomplete HTML tags. If the back-end has not set a standard link,{% tdk with name="CanonicalUrl" %}will return an empty string, causing the output to be similar to<link rel="canonical" href="" />The label.Although most browsers and search engines will ignore this invalid tag, from the perspective of code readability and cleanliness, conditionally outputting can maintain the clarity of the HTML structure, avoid potential parsing ambiguity, and conform to SEO practices.