没有不值得去解决的问题,也没有不值得去学习的技术!

WordPress Tag Cleanup (Part 4): Merge Chinese and English Tags and Automate URL Redirects With a Go Script

Figure 7: Shows the successful record of the old URL 301 redirecting to the new URL in the browser developer tools Network panel

When merging WordPress multilingual tags, the URLs of old tags face the issue of returning 404 errors. This not only affects user experience but also negatively impacts SEO. This article will share how to write a Go script that combines logs and dictionaries to automatically generate Nginx redirect rules, and resolve common pitfalls such as Chinese URL matching and trailing slash compatibility.

Project open-source address: https://github.com/shuijingwan/tag-merge

1. Data Preparation and Merge Log Analysis

The source of all redirects comes from the tag merge operation. Our merge script records the executed operations in a log, clearly specifying which source tags (IDs) were merged into which target tags (IDs).

Figure 1: Shows the operation records in merge_log.txt, such as: Preparing to process: [CN]权限(ID:27)->[CN]permission(ID:1519)
Figure 1: Shows the operation records in merge_log.txt, such as: Preparing to process: [CN]权限(ID:27)->[CN]permission(ID:1519)

At the same time, we need a complete tag dictionary file that records the mapping between tag IDs and URL Slugs, which is the key basis for generating redirect links.

Figure 2: Shows the contents of the all_terms_slug.csv file, including the correspondence between term_id and slug
Figure 2: Shows the contents of the all_terms_slug.csv file, including the correspondence between term_id and slug

2. Go Script Development and Pitfall Records

To convert the above logs and dictionary into a configuration usable by Nginx, we wrote the cmd/nginx-redirect/main.go script. During development, we primarily solved the following three core issues:

  1. URL encoding conversion: The $uri variable in Nginx obtains the decoded path. If you directly match %e6%9d%83%e9%99%90 in a regular expression, it will fail. Therefore, when reading the CSV in Go, you need to use the PathUnescape method of net/url to convert the Slug into Chinese characters (such as 权限) before concatenating the rules.
  2. Trailing slash compatibility: Users may access the URL with or without a trailing slash (such as /tag/redis缓存 and /tag/redis缓存/). The regular expression match needs to be adjusted from /$ to /?$ to ensure all variations are intercepted and uniformly redirected to the canonical URL with a trailing slash.
  3. Syntax context control: Ensure that the generated map block and server block have a clear hierarchy.
Figure 3: Shows the project structure, script execution terminal, and generated configuration preview in Trace CN
Figure 3: Shows the project structure, script execution terminal, and generated configuration preview in Trace CN

3. Generating Nginx Map Matching Rules

After running the Go script, it will output a nginx_redirect.conf file containing the map directive and the redirect logic. This file utilizes Nginx’s highly efficient map directive to map the matched old URIs into the variable $new_tag_uri.

Figure 4: Shows the specific generated rules, such as ~^/tag/指纹/?$ "/tag/fingerprint/"; etc.
Figure 4: Shows the specific generated rules, such as ~^/tag/指纹/?$ “/tag/fingerprint/”; etc.

4. Nginx Configuration Integration

Once you have the generated configuration file, you need to integrate it correctly into the website’s main Nginx configuration. There is a crucial syntax rule here: the map directive belongs to the http context and must be placed outside the server block, while the specific if condition and return redirect logic must be placed inside the server block.

Figure 5: Shows that in www.shuijingwanwq.com.conf, the include rule file is located before server {
Figure 5: Shows that in www.shuijingwanwq.com.conf, the include rule file is located before server {
Figure 6: Shows that inside the server block, the redirect is executed via if ($new_tag_uri != "") { return 301 $new_tag_uri; }
Figure 6: Shows that inside the server block, the redirect is executed via if ($new_tag_uri != “”) { return 301 $new_tag_uri; }

5. Effect Verification

After reloading the configuration, let’s verify the effect. When accessing the old Chinese tag URL (such as https://www.shuijingwanwq.com/tag/%E6%94%AF%E4%BB%98%E5%AE%9D/, meaning “Alipay”), the browser’s Network panel clearly shows: the server successfully responded with a 301 status code and instantly redirected to the new canonical English path https://www.shuijingwanwq.com/tag/alipay/, allowing the SEO authority to be perfectly passed on.

Figure 7: Shows the successful record of the old URL 301 redirecting to the new URL in the browser developer tools Network panel
Figure 7: Shows the successful record of the old URL 301 redirecting to the new URL in the browser developer tools Network panel
系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系