URL 使用 PCRE 表达式重写 - 将前缀附加到除一种模式之外的所有传入 URI

URL rewrite using PCRE expression - append prefix to all incoming URIs except one pattern

我正在使用匹配表达式作为 https://([^/]*)/(.*) 并将表达式替换为 constantprefix/$2 并尝试通过添加 '/constantprefix' 来重写传入的 URL所有 URLs

低于 URLs 它按预期工作:

  1. https://hostname/incomingURI 正在转换为 /constantprefix/incomingURI
  2. https://hostname/ 正在转换为 /constantprefix/
  3. https://hostname/login/index.aspx 正在转换为 /constantprefix/login/index.aspx

我遇到了已经以 /constantprefix 开头的 URLs 的问题,我在输出 URL 中看到两个 /constantprefix/constantprefix 我不是在寻找,是吗我们有什么办法可以避免这种情况?

如果传入 URL 是 https://hostname/constantprefix/login/index.aspx then output URL is becoming https://hostname/constantprefix/constantprefix/login/index.aspx 我可以知道如何从匹配表达式中避免 /constantprefix/constantprefix 吗?

您可以使用:

https://[^/]*/(?!constantprefix(?:/|$))(.*)

使用替换字符串:

constantprefix/

(?!...) 是一个否定的前瞻,意味着 后面没有 。这只是一个测试,不消耗字符(模式中的这种元素也称为 "zero-width assertions" 作为后视或锚点 ^$).

你模式中的第一个捕获组没用,我删除了它。