Satys
Satys

Reputation: 2425

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.

Upvotes: 4

Views: 2480

Answers (1)

Satys
Satys

Reputation: 2425

I managed to do it from here.

It basically needed two rewrites.

rewrite ^ $request_uri;
rewrite /.*/(.*)/(.*) /$1/$2;

The first rewrite modifies uri from /index.php -> /a/b/c.

The second rewrite modifies uri from /a/b/c -> /b/c.

Upvotes: 3

Related Questions