In website operations, the standardization of external links is often overlooked but is also very important.The prefix displayed before external links on a unified page, which not only helps enhance the professional image of the website, but also optimizes SEO performance to some extent, and even facilitates subsequent data analysis or link management.replaceThe filter is the tool to achieve this goal.
UnderstandreplaceFilter
In the template system of AnQiCMS,replaceA filter is a very practical string processing tool, its main function is to help us find and replace specific substrings in a string. Its basic syntax is very intuitive:{{ obj|replace:"旧字符串,新字符串" }}.
Here,objIs the original string or variable you want to perform the replacement operation followed byreplaceAfter the filter, we need to use the English comma,Separate 'old string' and 'new string'. The system will traverseobjAll content inside will match and replace 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 directly removed.
The necessity of unifying the prefix of external links
You may have encountered such a situation: the content of the website contains some links pointing to external resources, but the prefixes of these links may be diverse, such as some beinghttp://starting withhttps://Even some point to outdated domains. This inconsistency may cause some problems:
- SEO impact: Search engines prefer secure and standard links.Content mixed with HTTP and HTTPS may reduce the authority of the page, and links to old domains may lead to 404 errors, damaging user experience and website weight.
- User Experience:A link address that is not unified may confuse users and even cause doubts about the professionalism of the website.
- Brand unifiedFor websites with a clear brand strategy, ensuring that all external links point to or go through a specified prefix is part of maintaining the brand image.
- Data analysisIf you need to track user behavior when clicking external links, a unified link prefix can simplify the analysis work.
PassreplaceFilter, we can easily process these links in batches, achieving a unified prefix for external links on the page.
Actual Operation: Modify the template file
In AnQiCMS, content is usually displayed through template tags (such asarchiveDetail/pageDetailcome to show. We just need to find the template file that renders this content, and apply the variable to the output link.replacethe filter.
Assuming you want to replace all content on the page that starts withhttp://old-domain.comThe external link at the beginning should be unified replaced withhttps://new-secure-domain.com.
Step 1: Locate the template file
通常,文章详情页面的内容会由archiveDetailthe tag is responsible for rendering, while the single page is bypageDetailLabel processing. According to the template design conventions of AnQiCMS, this content may be located in{模型table}/detail.html(such asarchive/detail.html) orpage/detail.htmletc. You need to find these files in the template theme directory you are using.
Step Two: Find the content output location
In these template files, you will see something similar{{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 (a 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 will replace all matchinghttp://including any internal links that may exist, so please make sure that all your internal links are HTTPS when using them.http://only appear in the 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, external URLs may also be included in friendship links, navigation links, etc. For example, if your friendship link list (linkListLabel generation) includes external links that require a unified prefix, you can replace them while iterating over 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:Please make a backup of your template file before making any template modifications to prevent any unexpected issues.
- Exact match:
replaceThe filter directly matches strings. If you only want to replace links under a specific domain, make sure to include the full domain in the 'old string', for examplehttp://old-domain.com/,instead of justold-domain.comto avoid mistakenly affecting other content. |safeThe order:)|safeThe filter should always be placed in the chained filterslast, to ensure that the final HTML content is marked as safe output after all replacement operations are completed.- Test verification:Please ensure to perform a thorough test on the website frontend page after modification, to ensure that the link replacement effect meets expectations and no new issues are introduced.
PassreplaceFilter, AnQiCMS provides a powerful and flexible way to unify and optimize the prefix of external links displayed on website pages, thereby enhancing the overall quality and operational efficiency of the website.
Common Questions (FAQ)
Q1: Usereplace[en]Does the unified link prefix of the filter affect the internal links of my website?
[en]A1: It depends on where youreplace[en]Set in the filter under "Old String". If the "Old String" you set ishttp://This very general prefix, and some of the internal links on your website are stillhttp://The protocol, so these internal links may also be affected. But if you set the 'old string' to include the full domain name, for example,http://old-external-domain.comThen only links to this specific external domain will be replaced, thus avoiding affecting your internal links.Suggest to ensure that your internal links have fully adopted the HTTPS protocol before performing such operations.
Q2: I have multiple different external link prefixes (such ashttp://a.comandhttp://b.com) and I want to unify them to the same new prefix (such ashttps://new.com). Can I do it all at once?
A2: It's okay. You can apply multiple operations to the same variable in a chain.replacefilter. For example:
{{ archiveContent
|replace:"http://a.com,https://new.com"
|replace:"http://b.com,https://new.com"
|safe
}}
In this way, the system will firsthttp://a.comwithhttps://new.com, and thenhttp://b.comwithhttps://new.com. Please note the execution order of the filters, from left to right.
Q3: How should I ensure that only external links are converted to HTTPS while leaving internal links unaffected if I only want to force all HTTP protocol links to HTTPS?
The most ideal way is to ensure that internal links use relative paths during content editing