2

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

对于以下 URL,它按预期工作:

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

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

如果传入的 URL 是https://hostname/constantprefix/login/index.aspx那么输出 URL 正在变成https://hostname/constantprefix/constantprefix/login/index.aspx 我可以知道如何避免 /constantprefix/constantprefix 匹配表达 ?

4

1 回答 1

5

你可以这样做:

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

使用替换字符串:

constantprefix/$1

(?!...)是负前瞻,表示不跟随. 它只是一个测试并且不消耗字符(模式中的这种元素也称为“零宽度断言”作为后视或锚点^$)。

您模式中的第一个捕获组没用,我将其删除。

于 2016-10-12T19:53:02.653 回答