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

700 Views Asked by At

i am using match expression as https://([^/]*)/(.*) and replace expression as constantprefix/$2 and trying to rewrite incoming URL by adding '/constantprefix' to all URLs

for Below URLs it is working as expected:

  1. https://hostname/incomingURI is converting to /constantprefix/incomingURI
  2. https://hostname/ is converting to /constantprefix/
  3. https://hostname/login/index.aspx is converting to /constantprefix/login/index.aspx

i am having problem for the URLs which already starting with /constantprefix, i am seeing two /constantprefix/constantprefix in the output URL which I am not looking for, is there any way we can avoid that ?

if incoming URL is https://hostname/constantprefix/login/index.aspx then output URL is becoming https://hostname/constantprefix/constantprefix/login/index.aspx may i know how i can avoid /constantprefix/constantprefix from match expression ?

1

There are 1 best solutions below

0
Casimir et Hippolyte On BEST ANSWER

You can do it with:

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

using the replacement string:

constantprefix/$1

(?!...) is a negative lookahead and means not followed by. It's only a test and doesn't consume characters (this kind of elements in a pattern are also called "zero-width assertions" as a lookbehind or anchors ^ and $).

The first capture group in your pattern was useless, I removed it.