In-depth analysis: How to cleverly obtain the full URL address of the current page in Anqi CMS template?

As an experienced website operation expert, I know that accurately obtaining the complete URL address of the current page is a common and important need in daily work. Whether it is to set standardizedcanonicalTags, sharing to social media, passing parameters, or performing data statistics, the accurate URL of a page is core information. In AnQiCMS, an efficient and SEO-friendly content management system, there is no direct term calledcurrent_page_urlSuch a global variable, but through its flexible template tag system, we can still easily and accurately obtain the required information.

The AnqiCMS uses a template engine syntax similar to Django, emphasizing simplicity and practicality.It is designed intelligently to make the generation and management of content URLs simple and powerful, which is attributed to its deep integration with pseudo-static rules and content models.Therefore, to obtain the URL of the current page, we need to understand the URL construction logic of AnQiCMS and make good use of the various tags it provides.

Understand the URL construction philosophy of Anqi CMS

The highlight of AnQi CMS is its automatic and optimized URL processing.The system will dynamically generate the page access address based on the pseudo-static rules you configure in the background (such as numeric patterns, model naming patterns, or custom patterns), as well as the ID, alias, category, and other information of the content itself.This means that in most cases, you do not need to manually concatenate complex URLs, the system has already prepared standardized, SEO-friendly links for you.

This "out-of-the-box" link generation mechanism greatly simplifies the work of template developers. In the template, when you refer to some content (whether it is an article, product, category, or single page), you can usually directly go through its built-inLinkTo obtain the complete access path of the property.

The way to obtain the URL of the content page.

In Anqi CMS, for the purpose of.Content detail page./Category list page/single pageas well asTab pagePages driven by content models can be accessed relatively directly. The system will retrieve the corresponding object based on the context of the current page, such asarchive/category/page/tag)inject into the template environment, you just need to access these objects'Linkproperties.

For example, on the article detail page (for examplearchive/detail.htmlOr customize the article template) To get the current article URL, you can use it directly:

<a href="{{ archive.Link }}">访问当前文章</a>

HerearchiveThe variable represents the article object being accessed, and.LinkIt is the automatically generated complete access path (usually a path relative to the root directory of the website, or even a complete absolute path under some configurations).

Similarly, the principle is consistent in other types of pages:

  • Category page (category/list.htmlor custom category template):
    
    <a href="{{ category.Link }}">当前分类链接</a>
    
  • Single page (page/detail.htmlor custom single page template):
    
    <a href="{{ page.Link }}">当前单页面链接</a>
    
  • Tag detail page (tag/detail.html):
    
    <a href="{{ tag.Link }}">当前标签链接</a>
    

When you traverse the content in a list loop, obtaining the URL of each list item is also achieved byitem.Linkwhich is implemented. For example, in an article list:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>{{ item.Description }}</p>
    {% endfor %}
{% endarchiveList %}

Hereitemeach article object in the loop is,.LinkIt provided the access link for each article.

Build a complete URL: Basics and Variables

Althougharchive.LinkAttributes are usually already provided with a sufficiently complete path to the page, but in some special cases, you may need to obtain a URL that includes the protocol header and the full domain nameAbsolute URLfor example, used forhreflangTags, cross-domain API calls, or client-side JavaScript logic.

Anqi CMS passedsystemTags provide a series of basic configuration information for websites, including the root address of the website.BaseUrl. You can get it like this in the template:

{% system siteBaseUrl with name="BaseUrl" %}
<p>网站基础地址是: {{ siteBaseUrl }}</p>

BaseUrlIt usually returns the website address configured in the background “Global Settings”, for examplehttps://www.example.com. If you need to convert relative paths to absolute paths, you can use them togetherBaseUrl:

{% system siteBaseUrl with name="BaseUrl" %}
{% archiveDetail currentArchive with name="Link" %}
<a href="{{ siteBaseUrl }}{{ currentArchive }}">绝对文章链接</a>

However, please note: Due to AnQiCMS'sLinkProperties often already include the domain name, and direct concatenation may lead to duplicate URLs. Usually,archive.LinkThe URL that has already been returned is one that conforms to SEO standards and can be used directly. The safest way is to use the content object built-in first.LinkProperty.

Skillfully use auxiliary tags and scenario practice

For more professional URL requirements, AnQi CMS also provides powerfultdkThe tag is especially suitable for handling page SEO meta information, including canonical links (Canonical URL).

Set the canonical link (Canonical URL)

Set at the top of the pagecanonicalTags are an important part of SEO, it