3

This SO disucssion says that to forward parameters when rewriting an url using return the code should resemble this

location /path/to/resource {
  return 301 /some/other/path/$is_args$args;   
}

So far, so good. But how to add an arbitrary new parameter to the query string? For example id=1.

The solution must cover at least these three cases:

  1. The original request has no query parameters
  2. The original request has query parameters, but not the parameter being added
  3. The original request already has the query parameter being added
Community
  • 1
  • 1
Epigene
  • 3,634
  • 1
  • 26
  • 31

1 Answers1

0

To rewrite path you can use the rewrite key word like

location /path/to/resource {
rewrite /some/other/path/$is_args$args;   
}

To forward the params appending $args will append the query params if present and will be empty if no query params are passed.

For conditional addition of new param like id=1 then if construct can be used within the location like:

location /path/to/resource {
if($args !~* "id"){
rewrite /some/other/path/$is_args$args&id=1;  
} 
}

Above will append the 'id' field if not present in the incoming url.

sallu
  • 376
  • 1
  • 6
  • What happens if there are no query parameters? Won't it come out as "/some/other/path/?&id=1"? – Epigene May 23 '16 at 13:34
  • True.. But what i am trying to say is you can put multiple if conditions to check if the $args has any value and them form the rewrite url accordingly – sallu May 23 '16 at 13:38