How to modify $uri to part of uri from request_uri in nginx?

Suppose the value of $request_uri is /a/b/c . The current value of $uri is /index.php . Is this possible to change my $uri to /b/c .

I have tried this, which doesn't seem to be working,

if ($request_uri ~* /a/(.*)/(.*)){
  set $uri /$1/$2;
}

But this gives error of duplicate "uri" variable. I also tried,

if ($request_uri ~* /a/(.*)/(.*)){
  rewrite ^ /$1/$2 break;
}

But $variables don't seem to store values.

Is there a way out? Thanks.


Solution 1:

Can you just use a temporary variable? Eg (not tried):

if ($request_uri ~* /a/(.*)/(.*)){
  set $tmp /$1/$2;
  rewrite ^ $tmp break;
}

or what about:

if ($request_uri ~* /a/(.*)/(.*)){
  rewrite ^ $request_uri;
  rewrite ^/[^/]*/(.*)/(.*) /$1/$2 break;
}

Solution 2:

As answered by @MTeck in https://stackoverflow.com/questions/9084969/nginx-request-uri-without-args

If you really want exactly $request_uri with the args stripped, you can do this.

local uri = string.gsub(ngx.var.request_uri, "?.*", "")

You will need to have lua available but that will do exactly what you're asking.