As a senior CMS website operation personnel in the security industry, I am well aware of the importance of content optimization and search engine friendliness for the success of the website.The canonical URL is an indispensable part of SEO strategy, it can effectively solve the problem of content duplication and ensure that search engines correctly identify the authoritative version of the page.When using Anqi CMS for template development and content publishing, we often need to flexibly handle these details.
Understand Canonical URL and its importance
Canonical URL (standard link) is an HTML<link>tag, itsrelset the attribute to"canonical". It is used to inform search engines that although the current page can be accessed through multiple URLs (such as those with different parameters, session IDs, or multiple similar content pages), which URL is the "preferred" or "canonical" version of the content.Using Canonical URL correctly can avoid search engines from penalizing the website for duplicate content, concentrate the ranking signals of the page, thereby improving the SEO effect.For websites that handle a large amount of content, may have content variants, or parameterized URLs, Canonical URL is the key to ensuring the healthy ranking of the website.
Set Canonical URL in AnQiCMS
The AnQi CMS provides good support for Canonical URL in the content management level.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.Please fill in here withhttp://orhttps://The complete link at the beginning, make sure it points to the final preferred version of the content. If not set, Anqí CMS will usually generate one according to the default URL structure of the page.
Use{% tdk %}Tag calling Canonical URL
In the AnQi CMS template system,{% tdk %}Tags are a powerful tool used to dynamically retrieve the TDK (Title, Description, Keywords) information of a page, as well as including the canonical link (Canonical URL). When we want to display the<head>You can use this tag when outputting the area specification link.
The basic calling method is as follows:
<link rel="canonical" href="{% tdk with name="CanonicalUrl" %}" />
However, calling it directly may pose a problem: if the backend has not set a standard link for the current page,{% tdk with name="CanonicalUrl" %}It will return an empty string. This may result in an empty in HTML<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 output conditionally
To ensure that output is only made when there is an actual Canonical URL value<link>Label, we should introduce a judgment mechanism in the template.The AnQi CMS template engine supports variable assignment and conditional judgment, making it simple and elegant to determine if a link exists.
First, we can use{% tdk %}The tag will assign the Canonical URL value to a temporary variable. Then, through a{% if %}Condition check, check if the variable is empty. If the variable contains a valid URL, we will output the complete<link rel="canonical">.
This is the recommended template code snippet, which shows how to determine if a standard link exists and perform conditional output:
{%- 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.CanonicalUrlvalue and assign it to a variable namedcanonical.{%-The use is to eliminate the white space before and after the tag line, keeping the HTML output neat.{%- if canonical %}: This is a conditional judgment statement. IfcanonicalThe variable is successfully assigned and its value is not empty (i.e., the background has set the specification link), thenifThe content within the block will be rendered.<link rel="canonical" href="{{canonical}}" />: This is the actual output specification link HTML tag, itshrefThe value of the attribute comes fromcanonicalVariable.{%- endif %}: The end tag of conditional judgment is also used-%}to avoid blank characters.
In this way, we ensure that the HTML of the page only includes the corresponding links when they are actually defined<link rel="canonical">Labels, avoid unnecessary or empty labels, making the template code more robust and professional.This is not only a good coding habit, but also further improves the website's search engine optimization quality.
Frequently Asked Questions
What will Anqi CMS handle if I do not set the Canonical URL?If you do not explicitly set the specification link in the background content management, AnQi CMS will usually use the actual URL of the current page as the default specification link.This means that, without additional configuration, the search engine will still consider the current visited 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?Based on the backend feature design of AnQi CMS, the canonical link (Canonical URL) is mainly set in the "Other Parameters" section of "Add Document" or "Edit Document".This means that it mainly targets document content such as articles or products.For category pages or single pages, although{% tdk %}tags can be obtainedCanonicalUrlBut the background may not provide a direct setting entry to define them independently.In this case, the system default URL structure usually acts as the standard URL, or you may need to expand this feature through custom development.
Why do I need to judge in the template?canonicalDoes the variable exist instead of directly outputting{% tdk with name="CanonicalUrl" %}?Judging whether the variable exists is to avoid generating empty or incomplete HTML tags. If the backend has not set a standard link,{% tdk with name="CanonicalUrl" %}It will return an empty string, resulting in output similar to<link rel="canonical" href="" />The tag. Although most browsers and search engines will ignore this invalid tag, from the perspective of code standardization and cleanliness, conditionally outputting can maintain the clarity of the HTML structure, avoid potential parsing ambiguities, and comply with SEO practices.