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
  • 34,499
  • 3
  • 38
  • 58
  • can this be used in a map file / formatting? like this one [link](http://serverfault.com/questions/441235/maintaining-redirects-in-nginx-from-an-external-source) – Oscar Yuandinata Oct 19 '16 at 03:54
  • Ah, I misunderstood your question and where you were actually using the regular expressions. I'll update the answer once I get more information of the use. – Tero Kilkanen Oct 19 '16 at 18:05