0

Preset:

We need to substitute a query parameter t=http://endpoint/:df01:/:df02:/:df03:/004.jpg with other query parameters ...?:df01:=1&:df02:=13&:df03:=9 we are passing in the request.

Result:

Redirect the request to http://endpoint/1/13/9/004.jpg.

By now we have a rule in our nginx.conf working well by executing the following rule :

    if ($args ~ "^rt=rt0010_01&.*&:df\d+:=(\d+)&:df\d+:=(\d+)&:df\d+:=(\d+)&t=(https?:\/\/.*)\/Newsletter\/.*\/.*\/.*\/(.*)") {
        set $location $4;
        set $params $2/$1/$3/$5;
        rewrite . $location/Newsletter/$params? redirect;
    }

Our problem is that we cannot dynamically change the arguments in the t= path like t=http://endpoint/:df01:/:df03:/:df02:/004.jpg, because the params argument is set "hardcoded" to $2/$1/$3/$5.

So my question is, can I build some kind of rewrite rule which parses the :dfxx: params in the t= variable and then substitute it with the other params suppied in the request or do I have to include some module extension which perhaps can do the job? (Btw, we also have to solve the hardcoded /Newsletter in the rewrite , it is yet present in the url supplied by the t=... param).

I was asked to add an example containing a full request and a corresponding response:

REQUEST:

http://nginx-srv.com/rd/?rt=rt0010_01&cid=609&:df01:=609&:df26:=02&:df08:=1&t=http://my.domain/Newsletter/:df26:/:df01:/:df08:/008.jpg

RESPONSE:

http://my.domain/Newsletter/02/609/1/008.jpg

I hope this is sufficient.

1
  • Could you please show a full request and the desired rewrite? Commented Jan 16, 2018 at 17:35

1 Answer 1

2

With the following rules you can shift the df parameters, and the endpoint is variable.

      if ($args ~ "(.*&?):(df\d\d):=(\d+)(&?.*/):\2:(/.*)" ) {
        set $args  $1$4$3$5;
      }
      if ($args ~ "(.*&?):(df\d\d):=(\d+)(&?.*/):\2:(/.*)" ) {
        set $args  $1$4$3$5;
      }
      if ($args ~ "(.*&?):(df\d\d):=(\d+)(&?.*/):\2:(/.*)" ) {
        set $args  $1$4$3$5;
      }
      if ( $args
           ~ "&?t=(https?:\/\/[^/]*/)([A-Za-z]*)(\/.*\/.*\/.*\/)([^&]*)&?" ) {
        set $rscheme $1;
        set $endpoint $2;
        set $arguments $3;
        set $image $4;
      }
      rewrite . $rscheme$endpoint$arguments$image? redirect;
      return 301;

An URL with the parameters

:df01:=1&:df02:=13&:df03:=9&t=http://my.domain/Newsletter/:df01:/:df02:/:df03:/004.jpg

gets rewritten to

http://my.domain/Newsletter/1/13/9/004.jpg

With parameters

:df02:=13&:df01:=1&:df03:=9&t=http://your.domain/Letternews/:df02:/:df03:/:df01:/lookatme.jpg

it gets redirected to

http://your.domain/Letternews/13/9/1/lookatme.jpg
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Gerard, i simply have a problem to run it. I included your code in a location rd / {} block (the root url alwys starts with it) and nginx always responds with 302 found, but my browser is actually not doing any redirect. What am I doing wrong? (Btw., your rule looks far better than mine :-))
I suppose you have other locations that are more specific and match your URL. Without your complete configuration, I'm only guessing. But it's clear my code returns 301. Run an nginx -T, and look for all locations and 302's. What is a location rd / ?
Hi! thanks for responding. location rd was the part following nginx-srv.com as first path portion of the url. I will give a collegue a try. One more question: How would you implement some kind of dynamic path substitution in the t= param like t=your.domain/:df01:/:df02:/someImage.jpg in contrast to t=your.domain/:df01:/someImage.jpg, so that the third regexp group (\/.*\/.*\/.*\/) we have in t= can vary?
"location rd /" gives nginx: [emerg] invalid location modifier "rd". Regarding your one more question: I'd have a word with the designer of your site, and put him in a straitjacket.
put "&?t=(https?:\/\/[^/]*/)([A-Za-z]*)(\/[^/]*(\/[^/]*)*/)([^&]*)&?" and set $image $5;, then you can have less than 3 parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.