3

I don't know if this question is allowed or not. If not, forgive me :)
Anyway, I have a mapping rule for nginx redirection

/hotel/xyz/abc  /hotel/xyz/abc-nana;
/hotel/xyz/abc/  /hotel/xyz/abc-nana;
~^/hotel/xyz/abc\?(.*) /hotel/xyz/abc-nana?$1;

My question is, can they be combined into 1 rule? I don't know regex well

1 Answers1

0

I assume xyz and abc mean any alphanumeric string. Then you are looking for a rule like this:

location ~ ^/hotel/([0-9a-z]+)/([0-9a-z]+)/?$ {
    rewrite ^ http://$host/hotel/$1/$2-nana$is_args$args permanent;
}

(0-9a-z]+) matches any alphanumeric string having one or more matches. The results are stored into $1 and $2 variables. The last slash match is optional (? specifies 0 or 1 matches).

In nginx, one cannot match the query arguments in location or rewrite statements. However, if you only want to add possible query arguments without modification to the redirect, then $is_args$args is enough for that.

Tero Kilkanen
  • 38,887