You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js 6.10正则精确匹配问题:避免循环与部分匹配求助

Fixing Exact Match Redirects in Nginx (No More Unintended Matches!)

Hey there! I see you're dealing with unintended prefix matches and potential loops in your redirect rules—let's get this sorted out properly. The core issue with your current setup is that you're missing the end-of-string anchor ($) in your regular expressions, which means Nginx is matching any path that starts with your pattern, not just the exact path you want.

Here's the corrected set of rules that will do exactly what you need:

# Exact match for /test (no trailing slash, no extra characters)
^/test$ /test-videos.html [R=302,L]

# Exact match for /test/ (with trailing slash)
^/test/$ /test/videos.html [R=302,L]

# Exact match for /test123 (no extra characters)
^/test123$ /test/test-videos.html [R=302,L]

Let's break down why this works:

  • The $ at the end of each regex tells Nginx: "Only match this pattern if it's the entire path." So ^/test$ won't match /test123 (since the path ends with 123, not test) or /test/ (since it ends with a slash).
  • The L flag ensures that once a rule is matched, Nginx stops processing further rules—no more unexpected overlaps or loops.
  • The order here doesn't cause conflicts because each rule is strictly exact: /test123 can't be matched by the first rule, since its path doesn't end with test.

Let's verify with your request examples:

  • GET www.domain.com/test → Matches the first rule, redirects to /test-videos.html
  • GET www.domain.com/test/ → Matches the second rule, redirects to /test/videos.html
  • GET www.domain.com/test123 → Matches the third rule, redirects to /test/test-videos.html
  • GET www.domain.com/testabc → Doesn't match any rule (as expected)

This setup eliminates the prefix matching issues and ensures each redirect only triggers for the exact path you specify.

内容的提问来源于stack exchange,提问作者Vivek Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:55:00