Score:0

Append URL parameter to request URI using Nginx

ng flag

I'm trying to append URL parameters to specific request URI's within the server block.

This is what I have so far:

if ( $request_uri = "/testing/signup" ) {
    rewrite ^ https://www.example.com/testing/signup?org=7689879&type_id=65454 last;
}

location /testing/ {
    try_files $uri $uri/ /testing/index.php;
}

However, this only works when the original request URI does not have any of it's own URL parameters (e.g. www.example.com/testing/signup?abc=hello) I want to keep the original URL params and add my own.

I've tried changing the regex to if ( $request_uri ~* "^/testing/signup" ) { but this causes a loop.

Can anyone help?

**** UPDATE ****

I've updated to try this:

location /testing/ {
    rewrite ^/testing/signup$ /testing/signup?org=1231564 break;
    try_files $uri $uri/ /testing/index.php$is_args$args;
}

This doesn't pass the URL parameters but when checking the logs can see that both the existing URL param and the new one are in the args variable. But how do I get these into the GET request to the server can act on them?

2021/08/03 02:27:07 [notice] 3202#3202: *27 "^/testing/signup$" matches "/testing/signup", client: 146.75.168.54, server: example.com, request: "GET /testing/signup?id=1 HTTP/2.0", host: "www.example.com"
2021/08/03 02:27:07 [notice] 3202#3202: *27 rewritten data: "/testing/signup", args: "org=1231564&id=1", client: 146.75.168.54, server: example.com, request: "GET /testing/signup?id=1 HTTP/2.0", host: "www.example.com"
sv flag
Welcome to ServerFault. You have to use `$uri` in the `if` condition. Btw, `$request_uri` contains the arguments as well. That's why your code doesn't work if the request contains its own parameters such as `www.example.com/testing/signup?abc=hello`. In general, original parameters are appended at the end when we try to add our own parameters.
Score:1
sv flag

Welcome to ServerFault.

The variable request_uri contains "full original request URI (with arguments)". That's why a request with existing parameter didn't work for the original code. Instead, we could use uri that is normalized. So, the following code would work...

if ( $uri = "/testing/signup" ) {
    rewrite ^ https://www.example.com/testing/signup?org=7689879&type_id=65454 last;
}

location /testing/ {
    try_files $uri $uri/ /testing/index.php;
}

Please know that the original parameters are appended to our manually added parameters. So, for a request like www.example.com/testing/signup?abc=hello, the URI is rewritten into www.example.com/testing/signup?org=7689879&type_id=65454&abc=hello.

Michael Hampton avatar
cz flag
This will work perfectly. Of course it will also result in an infinite redirect loop because he has redirected back to the same URL path. But that's a different question entirely.
Neil Simpson avatar
ng flag
Yeah that's the problem, I keep getting into a loop. So is there a solution that won't cause a loop?
sv flag
That's a different question. Please create another question and post the full configuration to look into the infinite loop issue.
Neil Simpson avatar
ng flag
Not really a different question, so it would get blocked. The issue of looping is in the original question and a solution that causes a loop is not really a solution. Your solution does not work @pothi-kalimuthu
sv flag
@NeilSimpson I had a chance to look into the incomplete answer. Now, it's been improved to fix the infinite loop.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.