In website operation, the normativeness of external links is often overlooked but it is also very important.A uniform prefix displayed on the page for external links, which not only helps enhance the professional image of the website, but also to some extent optimizes the SEO performance, even facilitates subsequent data analysis or link management.AnQiCMS as a flexible content management system, provides powerful template engine features, wherereplaceThe filter is the tool to achieve this goal.

UnderstandreplaceFilter

In the AnQiCMS template system,replaceThe filter is a very practical string processing tool, its main function is to help us find and replace specific substrings in strings. Its basic syntax is very intuitive: {{ obj|replace:"旧字符串,新字符串" }}.

Here, objThe original string or variable that you want to perform the replacement operation follows.replaceAfter the filter, we need to use the English comma.,To separate the 'old string' and 'new string'. The system will traverseobjReplace all occurrences of the "old string" with the "new string".If the 'old string' is empty, it will match at the beginning of the original string and after each UTF-8 sequence;If the 'new string' is empty, the matched 'old string' will be removed directly.

The necessity of unifying the external link prefix

You might have encountered such a situation: the content of a website contains links to external resources, but these links may have a variety of prefixes, such as some starting withhttp://and some starting withhttps://, even some point to outdated domain names. This inconsistency may cause some problems:

  1. SEO impactThe search engine prefers secure and standardized links. Mixed content (HTTP and HTTPS coexisting) may reduce the authority of the page, and links pointing to old domain names may lead to 404 errors, damaging user experience and website authority.
  2. User Experience: An inconsistent link address may confuse users and even doubt the professionalism of the website.
  3. Brand UniformityFor websites with a clear brand strategy, ensuring that all external links point to or pass through a specified prefix is part of maintaining the brand image.
  4. Data analysis: If the behavior of users clicking external links needs to be counted, a unified prefix for links can simplify the analysis work.

ByreplaceFilter, we can easily batch process these links to unify the prefix of external links on the page.

Actual operation: modify the template file

In AnQiCMS, content is usually processed through template tags (such asarchiveDetail/pageDetailTo demonstrate. We just need to find the template file that renders this content, and apply the variables to the output link.replacethe filter.

Suppose you want to replace all the content on the page with a character that starts withhttp://old-domain.comReplace the external link at the beginning with a unified onehttps://new-secure-domain.com.

Step 1: Locate the template file

Generally, the content of the article detail page isarchiveDetailtag is responsible for rendering, while the single page ispageDetailLabel processing. According to the AnQiCMS template design convention, this content may be located{模型table}/detail.html(such asarchive/detail.htmlorpage/detail.htmlin the files. You need to find these files in the theme directory of the template you are using.

Step two: Find the content output location

You will see something similar in these template files{{archiveContent|safe}}or{{pageContent|safe}}Such code. This line of code is responsible for safely outputting the HTML content of an article or page to the page.|safeThe filter is very critical here, it tells the template engine that this content is safe HTML and does not require additional escaping. We need to|safeinsertreplacefilter.

Step three: insertreplaceFilter

Now, we modify the above code by addingreplacefilter.

Example 1: Replace a specific old HTTP domain with a new HTTPS domain

If you want to replace allhttp://old-domain.comlinks starting withhttps://new-secure-domain.comThis can be done:

{# 假设archiveContent变量包含了文章的HTML内容 #}
{{ archiveContent|replace:"http://old-domain.com,https://new-secure-domain.com"|safe }}

Example 2: Force all external links with HTTP protocol to be converted to HTTPS (common practice)

If your goal is to ensure that all external links use the HTTPS protocol, you can replace allhttp://the prefix withhttps://. Please note that this method will replace all matches.http://Including any internal links that may exist, therefore make sure that your internal links are all HTTPS orhttp://Only appear in external links that need to be replaced.

{# 强制将所有http://前缀转换为https:// #}
{{ archiveContent|replace:"http://,https://"|safe }}

Example 3: Handling other dynamically generated links

In addition to article content, friendship links, navigation links, etc. may also contain external URLs. For example, if your list of friendship links (bylinkListLabel generation) includes external links that need a unified prefix, you can replace them while traversing these links:

{% linkList friendLinks %}
    {% for item in friendLinks %}
        <a href="{{ item.Link|replace:"http://old-site.com,https://new-site.com" }}" target="_blank">{{item.Title}}</a>
    {% endfor %}
{% endlinkList %}

Important reminder:

  • Backup template:Before making any template modifications, please make sure to backup your template files to prevent any unexpected issues.
  • Exact match: replaceThe filter directly matches strings. If you only want to replace links under a specific domain, you must include the full domain in the 'old string', for examplehttp://old-domain.com/, rather than justold-domain.comto avoid mistakenly damaging other content.
  • |safethe order: |safeThe filter should always be placed in the chain of filtersFinallyto ensure that the final HTML content is marked as safe after all replacement operations are completed.
  • Test verification:After modification, please make sure to thoroughly test the website front-end page, ensuring that the link replacement effect meets expectations and no new problems have been introduced.

ByreplaceThe filter, AnQiCMS provides us with a powerful and flexible way to unify and optimize the display of external link prefixes on website pages, thereby enhancing the overall quality and operational efficiency of the website.


Frequently Asked Questions (FAQ)

Q1: UsereplaceDoes the unified link prefix filter affect the internal links of my website?

A1: It depends on where youreplaceSet the "old string" in the filter. If the "old string" you set ishttp://This very common prefix, and some of your internal website links are stillhttp://The protocol, then these internal links may also be affected. But if you set the 'old string' to include the full domain name, for examplehttp://old-external-domain.comThen only links matching this specific external domain will be replaced, thus avoiding affecting your internal links.Recommend that before performing such operations, ensure that your internal links are fully adopted HTTPS protocol.

Q2: Do I have multiple different external link prefixes (such ashttp://a.comandhttp://b.com) and want to unify them to a single new prefix (such ashttps://new.com), can I do it all at once?

A2: Sure. You can apply multiple operations to the same variable in sequence.replacea filter. For example:

{{ archiveContent
  |replace:"http://a.com,https://new.com"
  |replace:"http://b.com,https://new.com"
  |safe
}}

In this way, the system will first handlehttp://a.comReplacehttps://new.comThen it willhttp://b.comReplacehttps://new.comPlease note the execution order of the filters, from left to right sequentially.

Q3: If I only want to强制转换为HTTPS all the links of HTTP protocol, how can I ensure that only external links are converted and not internal links??

A3: The most ideal way is to ensure that internal links use relative paths during content editing