1

I'm having URLs like:

index.php?q=123&w=456&e=789

and I need to rewrite them in something like:

index/123/456/789

without using "if".

So far I've tried:

location / {
    rewrite ^/index.php\?q=(.*)&w=(.*)&e=(.*)$ /index/$1/$2/$3;
}

But it doesn't work. Any ideas?

Georgi Hristozov
  • 702
  • 1
  • 4
  • 13

1 Answers1

1

The short answer is you can't without using if.

The long answer is regex in the rewrite syntax only matchs the URI.

Try something like this:

    location /index.php {
        if ($args ~ "^q=(.*)&w=(.*)&e=(.*)") {
            set $arg_q $1;
            set $arg_w $2;
            set $arg_e $3;
            rewrite_log on;
        rewrite ^ /index/$arg_q/$arg_w/$arg_e last;
        }
    }
quanta
  • 50,327
  • 19
  • 152
  • 213