How to dynamically display the canonical URL corresponding to the current page in the template?

Calendar 👁️ 70

In website operation, maintaining search engine friendliness is one of the key factors in improving website visibility.Among them, the 'Canonical URL' plays a crucial role.It acts like a compass for search engines, telling them which page is the 'main version' of the content, thereby effectively avoiding ranking dispersion problems caused by content repetition or similarity.For content operators, correctly setting and displaying standard links is the foundation for ensuring that website content is efficiently crawled and indexed by search engines.

AnQiCMS (AnQiCMS) as a system focusing on enterprise-level content management and SEO optimization naturally considers the importance of standard links.When you are editing articles, products, or other documents in the background, you will find a field named "standard link" in the "other parameters" section of the document.Here you can manually specify a standard URL for the current page.This means that if you have pages with similar content, or if there are multiple access paths to a page (such as a URL with parameters), you can specify here which one is the authoritative version you want the search engine to index.This is especially useful for the multilingual versions of international sites or marketing pages with tracking parameters.

How do you dynamically present the specification link set up in the background in the HTML code of the website's frontend?Anqi CMS provides a simple and efficient template tag to achieve this.The standard link is usually placed on the page<head>Area, with<link rel="canonical" href="[您的规范链接]" />existing in the form of.

In the Anqi CMS template system, you can usetdktags to obtain the standard link of the page.tdkTags were originally designed to centrally manage page titles (Title), keywords (Keywords), and descriptions (Description) and also conveniently integrate with standard linksCanonicalUrlthe acquisition.

To dynamically display the specification link of the current page, you just need to add the following code snippet to the<head>part:

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

Let's explain the function of this code: First,{%- tdk canonical_url with name="CanonicalUrl" %}This line of code attempts to retrieve the value named from the meta information of the current pageCanonicalUrland assign it to a variable namedcanonical_urlthe variable. Here{%-and-%}Used to remove the blank lines generated by the tag line, making the generated HTML code cleaner. Next,{%- if canonical_url %}This line is a conditional judgment. Its purpose is to checkcanonical_urlIs the variable empty. Only when you fill in the content in the 'standard link' field in the background, will this variable have a value. Ifcanonical_urlthere is a value, then<link rel="canonical" href="{{ canonical_url }}" />This line of code will be rendered to the page where{{ canonical_url }}The actual specification link address will be replaced. This conditional rendering ensures that the tag will only be output on the page when there is a clear specification link, thus avoiding the output of an empty or unnecessary specification link tag.

You usually place this code in your common header template file (for exampletemplate/您的模板名/partial/header.htmlorbash.html)的<head>Label inside. So, regardless of which page calls this header template, it can automatically check and display the corresponding specification link.

In this way, Anqi CMS helps you easily manage the key SEO elements of your website.You can flexibly specify the canonical link for each page in the background, and the template will intelligently recognize and output this information to ensure that the search engine can accurately understand the authoritative version of your website content, thereby optimizing the performance of your website in search results.


Frequently Asked Questions (FAQ)

  1. Ask: Why doesn't my page display the standard link tags?Answer: First, please check whether the 'standard link' field in the 'other parameters' section of the relevant document edited in the Anqi CMS background is filled in correctly. If this field is empty, the conditional judgment in the template ({%- if canonical_url %}It will prevent its rendering. Next, make sure that you have correctly added the above code snippet to the template file.<head>And the template cache has been updated.

  2. Ask: Should I set the standard link on all pages?Answer: Not all pages require manual setting of the standard link.For pages with unique content and no other access path, search engines usually can correctly identify their authority.However, it is very important to set clear specifications for links when there are pages with highly similar content, multiple pages with parameterized URLs (such as filtering, sorting result pages), non-first pages in a series of paginated content, or when there is duplicate content on both PC and mobile ends of your website.It helps to concentrate the page authority to the main version you specify, avoiding the dispersion of rankings due to duplicate content.

  3. Ask: If I haven't manually set the 'standard link' field, will Anqi CMS automatically generate one?Answer: AnQi CMS will not automatically generate one if you do not set it manually<link rel="canonical">tag. It will prioritize respecting your explicit configuration in the background. If you do not fill in this field, throughtdkTags retrieved from comments.CanonicalUrlThe value will be empty, at this time the condition judgment in the template will ensure that this tag is not output. This allows you to fully control the standard link strategy of each page.

Related articles

How to prevent content collection interference code and watermark display in AnQi CMS?

Today, with the increasing richness of digital content, the value of original content becomes more and more prominent, but the problems of content plagiarism and collection that come with it also make many content creators and operators headache.AnQiCMS (AnQiCMS) fully understands this pain point, and therefore built-in strong content protection mechanisms from the very beginning of the system design, including anti-crawling interference codes and image watermark functions.These features are designed to help users effectively defend original copyright, ensure content exclusivity, and thereby enhance content value.

2025-11-08

How to display the document list matching the keywords on the search results page?

When visitors come to your website and hope to quickly find the information they need, a highly efficient and accurate on-site search feature is particularly important.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, providing you with a flexible and powerful content management and template system, allowing you to easily display a list of documents matching the search keywords on the search results page.Next, let's see how to implement this function together.### Learn about AnQi CMS search mechanism AnQi CMS has built-in powerful search capabilities, it will automatically index the content you publish

2025-11-08

How does AnQi CMS display or hide specific content based on user group permissions?

In daily website operation, we often encounter such needs: some content is intended for everyone to see, while some is only open to specific members, or shows different information for users of different levels.AnQiCMS (AnQiCMS) provides powerful user group permission management features, allowing us to flexibly control the visibility of website content and achieve the 'thousand faces of content'. ### Why do you need to display or hide content by user group?Imagine you are running a website that offers a variety of services, including free basic articles and paid in-depth reports

2025-11-08

How to dynamically display the current year or a specified date on a website?

In website operation, we often need to display some dynamic time information, such as the copyright year at the bottom of the website (such as “© 2023-Current Year”), the publication or update time of articles, or the deadline for specific events, etc.These dynamic dates not only maintain the timeliness of website information, but also improve user experience, and in some cases, are also beneficial for SEO. The AnQi CMS provides a series of flexible template tags that allow you to easily display the current year or a specified date dynamically at any location on the website.We will discuss in detail how to make use of these features.

2025-11-08

How to safely display HTML tags in a template without being parsed by the browser?

In website content management, we often encounter the need to display HTML code snippets, such as code examples in tutorials, and language annotations in technical articles.However, if the content containing HTML tags is directly output to the page, the browser will parse and render it, rather than displaying it as plain text.This could lead to layout errors on the page, more seriously, if unprocessed user input contains malicious scripts (such as `<script>alert('XSS')</script>`), it can trigger cross-site scripting (XSS) attacks

2025-11-08

How to display custom site parameters on the front-end page?

When operating a website, we often encounter such needs: we need to display some flexible and variable site information on the front-end page, such as the company's establishment year, customer service hotline, promotional slogans for specific pages, or a brief announcement, etc.This information may need to be updated frequently, and it is inefficient and prone to errors to modify the template file each time.Fortunately, Anqi CMS has provided us with a very convenient way to manage and display customized site parameters on the back-end, making content operation more efficient and flexible.

2025-11-08

How does AnQi CMS display the prompt information when the website is closed?

During the operation of the website, we often encounter situations where we need to maintain, upgrade the website, or temporarily close the website for some special reasons.How can you inform the visitor in a friendly manner about the current status of the website, to avoid them seeing a chaotic or error page, which is particularly important.AnQiCMS (AnQiCMS) fully considers this requirement, built-in convenient and flexible shutdown prompt function, allowing you to easily deal with such scenarios. ### Flexible website status configuration, easily enable shutdown mode The shutdown function of Anqi CMS is very intuitive to configure. You just need to log in to the website backend

2025-11-08

How to ensure that the website content of AnQi CMS adapts to display on different devices (PC, mobile)?

One of the most painful problems we face in running websites in this multi-screen era is how to make the content of the website display effectively on different devices.Users may browse using a PC, smartphone, or tablet. If the content does not adapt to display, the user experience will be greatly reduced.AnQiCMS (AnQiCMS) provides a very mature and flexible solution in this aspect, aiming to ensure that your website content looks pleasing on any device.### AnQiCMS' Various Content Adaptation Modes AnQiCMS understands that different websites have different needs

2025-11-08