Append URL parameter to request URI using Nginx
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"
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
.