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

sar*_*rma 2 regex urlrewriter pcre regex-greedy

我正在使用匹配表达式https://([^/]*)/(.*)并将表达式替换为constantprefix/$ 2并尝试通过向所有URL添加"/ constantprefix"来重写传入的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表达?

Cas*_*yte 5

你可以这样做:

https://[^/]*/(?!constantprefix(?:/|$))(.*)
Run Code Online (Sandbox Code Playgroud)

使用替换字符串:

constantprefix/$1
Run Code Online (Sandbox Code Playgroud)

(?!...)是一个消极的前瞻,意味着没有遵循.这只是一个测试和不消耗字符(这种在一个图案元素的也被称为"零宽度断言"作为回顾后或锚^$).

你模式中的第一个捕获组是无用的,我删除了它.